<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>MySQL8其它新特性 | 知识库</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/noteslibrary/img/favicon.ico">
    <meta name="description" content="Java后端技术博客,专注Java后端学习与总结。HTML,CSS,JavaScript,TypeScript,JavaSE,Python,Vue,React,Node,Git,Github等技术文章。">
    <meta name="keywords" content="后端博客,个人技术博客,后端,后端开发,后端框架,后端面试题,技术文档,学习,面试,HTML,CSS,JavaScript,TypeScript,JavaSE,Python,Vue,React,Node,Git,Github">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/noteslibrary/assets/css/0.styles.2500ff6d.css" as="style"><link rel="preload" href="/noteslibrary/assets/js/app.dc0c3d24.js" as="script"><link rel="preload" href="/noteslibrary/assets/js/2.a664539c.js" as="script"><link rel="preload" href="/noteslibrary/assets/js/105.235036be.js" as="script"><link rel="prefetch" href="/noteslibrary/assets/js/10.bab1a8b9.js"><link rel="prefetch" href="/noteslibrary/assets/js/100.245d2d1d.js"><link rel="prefetch" href="/noteslibrary/assets/js/101.9d1c6c13.js"><link rel="prefetch" href="/noteslibrary/assets/js/102.d187686f.js"><link rel="prefetch" href="/noteslibrary/assets/js/103.b0e7acf6.js"><link rel="prefetch" href="/noteslibrary/assets/js/104.895c9e53.js"><link rel="prefetch" href="/noteslibrary/assets/js/106.17a71ae1.js"><link rel="prefetch" href="/noteslibrary/assets/js/107.256ceb5d.js"><link rel="prefetch" href="/noteslibrary/assets/js/108.57832d85.js"><link rel="prefetch" href="/noteslibrary/assets/js/109.f8b1d1c7.js"><link rel="prefetch" href="/noteslibrary/assets/js/11.2c69801a.js"><link rel="prefetch" href="/noteslibrary/assets/js/110.f3ce6a68.js"><link rel="prefetch" href="/noteslibrary/assets/js/111.bdfd28f4.js"><link rel="prefetch" href="/noteslibrary/assets/js/112.878f599c.js"><link rel="prefetch" href="/noteslibrary/assets/js/113.0efa07cf.js"><link rel="prefetch" href="/noteslibrary/assets/js/114.c7aba251.js"><link rel="prefetch" href="/noteslibrary/assets/js/115.8b2e628d.js"><link rel="prefetch" href="/noteslibrary/assets/js/116.3392d8ba.js"><link rel="prefetch" href="/noteslibrary/assets/js/117.310538d0.js"><link rel="prefetch" href="/noteslibrary/assets/js/118.81eb015b.js"><link rel="prefetch" href="/noteslibrary/assets/js/12.e77f5a18.js"><link rel="prefetch" href="/noteslibrary/assets/js/13.d24d0054.js"><link rel="prefetch" href="/noteslibrary/assets/js/14.f29d4d33.js"><link rel="prefetch" href="/noteslibrary/assets/js/15.8694e081.js"><link rel="prefetch" href="/noteslibrary/assets/js/16.03ffb143.js"><link rel="prefetch" href="/noteslibrary/assets/js/17.83edd7b9.js"><link rel="prefetch" href="/noteslibrary/assets/js/18.58ca4447.js"><link rel="prefetch" href="/noteslibrary/assets/js/19.e136d5c2.js"><link rel="prefetch" href="/noteslibrary/assets/js/20.fd3961b6.js"><link rel="prefetch" href="/noteslibrary/assets/js/21.207406c6.js"><link rel="prefetch" href="/noteslibrary/assets/js/22.b8738ce2.js"><link rel="prefetch" href="/noteslibrary/assets/js/23.33e4529d.js"><link rel="prefetch" href="/noteslibrary/assets/js/24.ab5493c5.js"><link rel="prefetch" href="/noteslibrary/assets/js/25.2506ce48.js"><link rel="prefetch" href="/noteslibrary/assets/js/26.7e6a9c14.js"><link rel="prefetch" href="/noteslibrary/assets/js/27.e7b4e92a.js"><link rel="prefetch" href="/noteslibrary/assets/js/28.7ad46ba6.js"><link rel="prefetch" href="/noteslibrary/assets/js/29.81666f41.js"><link rel="prefetch" href="/noteslibrary/assets/js/3.8f13cd17.js"><link rel="prefetch" href="/noteslibrary/assets/js/30.07ada09b.js"><link rel="prefetch" href="/noteslibrary/assets/js/31.f271c8cf.js"><link rel="prefetch" href="/noteslibrary/assets/js/32.2350914c.js"><link rel="prefetch" href="/noteslibrary/assets/js/33.9d7bfb77.js"><link rel="prefetch" href="/noteslibrary/assets/js/34.6fcf6f6b.js"><link rel="prefetch" href="/noteslibrary/assets/js/35.0c3a88fb.js"><link rel="prefetch" href="/noteslibrary/assets/js/36.574ca92a.js"><link rel="prefetch" href="/noteslibrary/assets/js/37.72e38074.js"><link rel="prefetch" href="/noteslibrary/assets/js/38.16d408fb.js"><link rel="prefetch" href="/noteslibrary/assets/js/39.63abc4a8.js"><link rel="prefetch" href="/noteslibrary/assets/js/4.3e68fc94.js"><link rel="prefetch" href="/noteslibrary/assets/js/40.ca2bdd48.js"><link rel="prefetch" href="/noteslibrary/assets/js/41.f82c80ec.js"><link rel="prefetch" href="/noteslibrary/assets/js/42.d1b8f579.js"><link rel="prefetch" href="/noteslibrary/assets/js/43.d3fd6260.js"><link rel="prefetch" href="/noteslibrary/assets/js/44.491f9afa.js"><link rel="prefetch" href="/noteslibrary/assets/js/45.1ee0d084.js"><link rel="prefetch" href="/noteslibrary/assets/js/46.a3e0be1f.js"><link rel="prefetch" href="/noteslibrary/assets/js/47.647908d1.js"><link rel="prefetch" href="/noteslibrary/assets/js/48.9658b8b0.js"><link rel="prefetch" href="/noteslibrary/assets/js/49.8f4b8327.js"><link rel="prefetch" href="/noteslibrary/assets/js/5.a0a97ccd.js"><link rel="prefetch" href="/noteslibrary/assets/js/50.f46d1433.js"><link rel="prefetch" href="/noteslibrary/assets/js/51.f1b784a0.js"><link rel="prefetch" href="/noteslibrary/assets/js/52.cce45956.js"><link rel="prefetch" href="/noteslibrary/assets/js/53.d66dabe3.js"><link rel="prefetch" href="/noteslibrary/assets/js/54.384b864b.js"><link rel="prefetch" href="/noteslibrary/assets/js/55.47ed19f2.js"><link rel="prefetch" href="/noteslibrary/assets/js/56.193cd456.js"><link rel="prefetch" href="/noteslibrary/assets/js/57.e6ea1f8c.js"><link rel="prefetch" href="/noteslibrary/assets/js/58.97fd2330.js"><link rel="prefetch" href="/noteslibrary/assets/js/59.b0c3d9ea.js"><link rel="prefetch" href="/noteslibrary/assets/js/6.50cbd75f.js"><link rel="prefetch" href="/noteslibrary/assets/js/60.d01d0651.js"><link rel="prefetch" href="/noteslibrary/assets/js/61.385e9bae.js"><link rel="prefetch" href="/noteslibrary/assets/js/62.a93fa4c8.js"><link rel="prefetch" href="/noteslibrary/assets/js/63.f72a2142.js"><link rel="prefetch" href="/noteslibrary/assets/js/64.3bf0b024.js"><link rel="prefetch" href="/noteslibrary/assets/js/65.cb1cb3bb.js"><link rel="prefetch" href="/noteslibrary/assets/js/66.4c9ff8cd.js"><link rel="prefetch" href="/noteslibrary/assets/js/67.2fc17900.js"><link rel="prefetch" href="/noteslibrary/assets/js/68.fd3ee410.js"><link rel="prefetch" href="/noteslibrary/assets/js/69.682be05d.js"><link rel="prefetch" href="/noteslibrary/assets/js/7.80203dee.js"><link rel="prefetch" href="/noteslibrary/assets/js/70.29428a45.js"><link rel="prefetch" href="/noteslibrary/assets/js/71.aff6ef6b.js"><link rel="prefetch" href="/noteslibrary/assets/js/72.fe7572e0.js"><link rel="prefetch" href="/noteslibrary/assets/js/73.e52bc1a7.js"><link rel="prefetch" href="/noteslibrary/assets/js/74.5e72ee84.js"><link rel="prefetch" href="/noteslibrary/assets/js/75.5a5bba64.js"><link rel="prefetch" href="/noteslibrary/assets/js/76.70d6ff7c.js"><link rel="prefetch" href="/noteslibrary/assets/js/77.1db1c302.js"><link rel="prefetch" href="/noteslibrary/assets/js/78.137c92e8.js"><link rel="prefetch" href="/noteslibrary/assets/js/79.8455d34b.js"><link rel="prefetch" href="/noteslibrary/assets/js/8.9e13e493.js"><link rel="prefetch" href="/noteslibrary/assets/js/80.127f3a4e.js"><link rel="prefetch" href="/noteslibrary/assets/js/81.191d3614.js"><link rel="prefetch" href="/noteslibrary/assets/js/82.6ae31745.js"><link rel="prefetch" href="/noteslibrary/assets/js/83.640cd3d7.js"><link rel="prefetch" href="/noteslibrary/assets/js/84.22c036e2.js"><link rel="prefetch" href="/noteslibrary/assets/js/85.692cd496.js"><link rel="prefetch" href="/noteslibrary/assets/js/86.793e38a7.js"><link rel="prefetch" href="/noteslibrary/assets/js/87.eb27f1d6.js"><link rel="prefetch" href="/noteslibrary/assets/js/88.6d48c75e.js"><link rel="prefetch" href="/noteslibrary/assets/js/89.a798bacb.js"><link rel="prefetch" href="/noteslibrary/assets/js/9.7e864ff7.js"><link rel="prefetch" href="/noteslibrary/assets/js/90.e814aadc.js"><link rel="prefetch" href="/noteslibrary/assets/js/91.5c132772.js"><link rel="prefetch" href="/noteslibrary/assets/js/92.d0d1984d.js"><link rel="prefetch" href="/noteslibrary/assets/js/93.bbc81ca6.js"><link rel="prefetch" href="/noteslibrary/assets/js/94.242fbc29.js"><link rel="prefetch" href="/noteslibrary/assets/js/95.f7232d68.js"><link rel="prefetch" href="/noteslibrary/assets/js/96.440e6bbe.js"><link rel="prefetch" href="/noteslibrary/assets/js/97.a8ad3aa8.js"><link rel="prefetch" href="/noteslibrary/assets/js/98.e9a01f98.js"><link rel="prefetch" href="/noteslibrary/assets/js/99.e024f354.js">
    <link rel="stylesheet" href="/noteslibrary/assets/css/0.styles.2500ff6d.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/noteslibrary/" class="home-link router-link-active"><img src="/noteslibrary/img/EB-logo.png" alt="知识库" class="logo"> <span class="site-name can-hide">知识库</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/noteslibrary/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="基础" class="dropdown-title"><a href="/noteslibrary/basis/" class="link-title">基础</a> <span class="title" style="display:none;">基础</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《计算机网络》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《计算机组成原理》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《操作系统》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《数据结构》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《算法》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《设计模式》</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/noteslibrary/fontend/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/0796ba76b4b55368.html" class="nav-link">《JS笔记》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/f344d070a1031ef7.html" class="nav-link">《ES6笔记》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/note/vue/" class="nav-link">《Vue笔记》</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="后端" class="dropdown-title"><a href="/noteslibrary/backend/" class="link-title">后端</a> <span class="title" style="display:none;">后端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>JavaEE框架</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/d0b17d/" class="nav-link">《DAO全家桶》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/a9ac80/" class="nav-link">《Spring全家桶》</a></li></ul></li><li class="dropdown-item"><h4>BigData框架</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/efad7b/404" class="nav-link">《Hadoop生态》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/efad7b/404" class="nav-link">《Spark生态》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/efad7b/404" class="nav-link">《Flink生态》</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><a href="/noteslibrary/database/" class="link-title">数据库</a> <span class="title" style="display:none;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4></h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/32e9fd/" class="nav-link">MySQL</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="工具" class="dropdown-title"><a href="/noteslibrary/tools/" class="link-title">工具</a> <span class="title" style="display:none;">工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4></h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/3ac065/" class="nav-link">Maven</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/5760de/" class="nav-link">Git</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="收藏" class="dropdown-title"><a href="/noteslibrary/collect/" class="link-title">收藏</a> <span class="title" style="display:none;">收藏</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4></h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/50c072/" class="nav-link">博客收藏</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="其他" class="dropdown-title"><a href="/noteslibrary/others/" class="link-title">其他</a> <span class="title" style="display:none;">其他</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>博客部署</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/db737d/" class="nav-link">Hexo</a></li></ul></li><li class="dropdown-item"><h4>系统安装</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/3ac066/" class="nav-link">系统安装</a></li></ul></li><li class="dropdown-item"><h4>idea快捷键</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/111c73/" class="nav-link">idea快捷键</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/noteslibrary/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/noteslibrary/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/noteslibrary/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/noteslibrary/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/lcfqzd/vuepress-theme-vdoing" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><!----> <nav class="nav-links"><div class="nav-item"><a href="/noteslibrary/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="基础" class="dropdown-title"><a href="/noteslibrary/basis/" class="link-title">基础</a> <span class="title" style="display:none;">基础</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《计算机网络》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《计算机组成原理》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《操作系统》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《数据结构》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《算法》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《设计模式》</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/noteslibrary/fontend/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/0796ba76b4b55368.html" class="nav-link">《JS笔记》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/f344d070a1031ef7.html" class="nav-link">《ES6笔记》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/note/vue/" class="nav-link">《Vue笔记》</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="后端" class="dropdown-title"><a href="/noteslibrary/backend/" class="link-title">后端</a> <span class="title" style="display:none;">后端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>JavaEE框架</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/d0b17d/" class="nav-link">《DAO全家桶》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/a9ac80/" class="nav-link">《Spring全家桶》</a></li></ul></li><li class="dropdown-item"><h4>BigData框架</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/efad7b/404" class="nav-link">《Hadoop生态》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/efad7b/404" class="nav-link">《Spark生态》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/efad7b/404" class="nav-link">《Flink生态》</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><a href="/noteslibrary/database/" class="link-title">数据库</a> <span class="title" style="display:none;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4></h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/32e9fd/" class="nav-link">MySQL</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="工具" class="dropdown-title"><a href="/noteslibrary/tools/" class="link-title">工具</a> <span class="title" style="display:none;">工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4></h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/3ac065/" class="nav-link">Maven</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/5760de/" class="nav-link">Git</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="收藏" class="dropdown-title"><a href="/noteslibrary/collect/" class="link-title">收藏</a> <span class="title" style="display:none;">收藏</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4></h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/50c072/" class="nav-link">博客收藏</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="其他" class="dropdown-title"><a href="/noteslibrary/others/" class="link-title">其他</a> <span class="title" style="display:none;">其他</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>博客部署</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/db737d/" class="nav-link">Hexo</a></li></ul></li><li class="dropdown-item"><h4>系统安装</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/3ac066/" class="nav-link">系统安装</a></li></ul></li><li class="dropdown-item"><h4>idea快捷键</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/111c73/" class="nav-link">idea快捷键</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/noteslibrary/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/noteslibrary/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/noteslibrary/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/noteslibrary/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/lcfqzd/vuepress-theme-vdoing" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>MySQL笔记</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/noteslibrary/pages/32e9fd/" class="sidebar-link">写在前面</a></li><li><a href="/noteslibrary/pages/cfda94/" class="sidebar-link">数据库概述</a></li><li><a href="/noteslibrary/pages/497bcb/" class="sidebar-link">MySQL环境搭建</a></li><li><a href="/noteslibrary/pages/9c9f3a/" class="sidebar-link">基本的SELECT语句</a></li><li><a href="/noteslibrary/pages/b59830/" class="sidebar-link">运算符</a></li><li><a href="/noteslibrary/pages/6fef5b/" class="sidebar-link">排序与分页</a></li><li><a href="/noteslibrary/pages/3a50a8/" class="sidebar-link">多表查询</a></li><li><a href="/noteslibrary/pages/8de5b4/" class="sidebar-link">单行函数</a></li><li><a href="/noteslibrary/pages/2dfe6d/" class="sidebar-link">聚合函数</a></li><li><a href="/noteslibrary/pages/3ea5ac/" class="sidebar-link">子查询</a></li><li><a href="/noteslibrary/pages/70a90b/" class="sidebar-link">创建和管理表</a></li><li><a href="/noteslibrary/pages/a20c5b/" class="sidebar-link">数据处理之增删改</a></li><li><a href="/noteslibrary/pages/9515c0/" class="sidebar-link">MySQL数据类型精讲</a></li><li><a href="/noteslibrary/pages/98e681/" class="sidebar-link">约束</a></li><li><a href="/noteslibrary/pages/4535e0/" class="sidebar-link">视图</a></li><li><a href="/noteslibrary/pages/e5e604/" class="sidebar-link">存储过程与函数</a></li><li><a href="/noteslibrary/pages/f68b46/" class="sidebar-link">变量与流程控制与游标</a></li><li><a href="/noteslibrary/pages/fe941e/" class="sidebar-link">触发器</a></li><li><a href="/noteslibrary/pages/efad7b/" aria-current="page" class="active sidebar-link">MySQL8其它新特性</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/efad7b/#_1-mysql8新特性概述" class="sidebar-link">1. MySQL8新特性概述</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/efad7b/#_1-1-mysql8-0-新增特性" class="sidebar-link">1.1 MySQL8.0 新增特性</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/efad7b/#_1-2-mysql8-0移除的旧特性" class="sidebar-link">1.2 MySQL8.0移除的旧特性</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/efad7b/#_2-新特性1-窗口函数" class="sidebar-link">2. 新特性1：窗口函数</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/efad7b/#_2-1-使用窗口函数前后对比" class="sidebar-link">2.1 使用窗口函数前后对比</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/efad7b/#_2-2-窗口函数分类" class="sidebar-link">2.2 窗口函数分类</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/efad7b/#_2-3-语法结构" class="sidebar-link">2.3 语法结构</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/efad7b/#_2-4-分类讲解" class="sidebar-link">2.4 分类讲解</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/efad7b/#_2-5-小-结" class="sidebar-link">2.5 小 结</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/efad7b/#_3-新特性2-公用表表达式" class="sidebar-link">3. 新特性2：公用表表达式</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/efad7b/#_3-1-普通公用表表达式" class="sidebar-link">3.1 普通公用表表达式</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/efad7b/#_3-2-递归公用表表达式" class="sidebar-link">3.2 递归公用表表达式</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/efad7b/#_3-3-小-结" class="sidebar-link">3.3 小 结</a></li></ul></li></ul></li></ul></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/noteslibrary/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/noteslibrary/database/#数据库" data-v-06225672>数据库</a></li><li data-v-06225672><a href="/noteslibrary/database/#MySQL笔记" data-v-06225672>MySQL笔记</a></li></ul> <div class="info" data-v-06225672><!----> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2023-04-05</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">MySQL8其它新特性<!----></h1> <!----> <div class="theme-vdoing-content content__default"><h1 id="mysql8其它新特性"><a href="#mysql8其它新特性" class="header-anchor">#</a> MySQL8其它新特性</h1> <h2 id="_1-mysql8新特性概述"><a href="#_1-mysql8新特性概述" class="header-anchor">#</a> 1. MySQL8新特性概述</h2> <p><code>MySQL从5.7版本直接跳跃发布了8.0版本</code> ，可见这是一个令人兴奋的里程碑版本。MySQL 8版本在功能上做了显著的改进与增强，开发者对MySQL的源代码进行了重构，最突出的一点是多MySQL Optimizer优化器进行了改进。不仅在速度上得到了改善，还为用户带来了更好的性能和更棒的体验。</p> <h3 id="_1-1-mysql8-0-新增特性"><a href="#_1-1-mysql8-0-新增特性" class="header-anchor">#</a> 1.1 MySQL8.0 新增特性</h3> <p><strong>1.更简便的NoSQL支持</strong> NoSQL泛指非关系型数据库和数据存储。随着互联网平台的规模飞速发展，传统的关系型数据库已经越来越不能满足需求。从5.6版本开始，MySQL就开始支持简单的NoSQL存储功能。MySQL 8对这一功能做了优化，以更灵活的方式实现NoSQL功能，不再依赖模式（schema）。</p> <p><strong>2.更好的索引 在查询中</strong>，正确地使用索引可以提高查询的效率。MySQL 8中新增了 <code>隐藏索引</code> 和 <code>降序索引</code> 。隐藏索引可以用来测试去掉索引对查询性能的影响。在查询中混合存在多列索引时，使用降序索引可以提高查询的性能。</p> <p><strong>3.更完善的JSON支持</strong> MySQL从5.7开始支持原生JSON数据的存储，MySQL 8对这一功能做了优化，增加了聚合函数 <code>JSON_ARRAYAGG()</code>和 <code>JSON_OBJECTAGG()</code>，将参数聚合为JSON数组或对象，新增了行内操作符 -&gt;&gt;，是列路径运算符 -&gt;的增强，对JSON排序做了提升，并优化了JSON的更新操作。</p> <p><strong>4.安全和账户管理</strong> MySQL 8中新增了 <code>caching_sha2_password</code> 授权插件、角色、密码历史记录和FIPS模式支持，这些特性提高了数据库的安全性和性能，使数据库管理员能够更灵活地进行账户管理工作。</p> <p><strong>5.InnoDB的变化</strong> <code>InnoDB是MySQL默认的存储引擎</code> ，是事务型数据库的首选引擎，支持事务安全表（ACID），支持行锁定和外键。在MySQL 8 版本中，InnoDB在自增、索引、加密、死锁、共享锁等方面做了大量的 <code>改进和优化</code> ，并且支持原子数据定义语言（DDL），提高了数据安全性，对事务提供更好的支持。</p> <p><strong>6.数据字典</strong> 在之前的MySQL版本中，字典数据都存储在元数据文件和非事务表中。从MySQL 8开始新增了事务数据字典，在这个字典里存储着数据库对象信息，这些数据字典存储在内部事务表中。</p> <p><strong>7.原子数据定义语句</strong> MySQL 8开始支持原子数据定义语句（Automic DDL），即 <code>原子DDL</code> 。目前，只有InnoDB存储引擎支持原子DDL。原子数据定义语句（DDL）将与DDL操作相关的数据字典更新、存储引擎操作、二进制日志写入结合到一个单独的原子事务中，这使得即使服务器崩溃，事务也会提交或回滚。使用支持原子操作的存储引擎所创建的表，在执行DROP TABLE、CREATE TABLE、ALTER TABLE、RENAME TABLE、TRUNCATE TABLE、CREATE TABLESPACE、DROP TABLESPACE等操作时，都支持原子操作，即事务要么完全操作成功，要么失败后回滚，不再进行部分提交。 对于从MySQL 5.7复制到MySQL 8版本中的语句，可以添加 IF EXISTS 或 IF NOT EXISTS 语句来避免发生错误。</p> <p><strong>8.资源管理</strong> MySQL 8开始支持创建和管理资源组，允许将服务器内运行的线程分配给特定的分组，以便线程根据组内可用资源执行。组属性能够控制组内资源，启用或限制组内资源消耗。数据库管理员能够根据不同的工作负载适当地更改这些属性。 目前，CPU时间是可控资源，由“虚拟CPU”这个概念来表示，此术语包含CPU的核心数，超线程，硬件线程等等。服务器在启动时确定可用的虚拟CPU数量。拥有对应权限的数据库管理员可以将这些CPU与资源组关联，并为资源组分配线程。 资源组组件为MySQL中的资源组管理提供了SQL接口。资源组的属性用于定义资源组。MySQL中存在两个默认组，系统组和用户组，默认的组不能被删除，其属性也不能被更改。对于用户自定义的组，资源组创建时可初始化所有的属性，除去名字和类型，其他属性都可在创建之后进行更改。 在一些平台下，或进行了某些MySQL的配置时，资源管理的功能将受到限制，甚至不可用。例如，如果安装了线程池插件，或者使用的是macOS系统，资源管理将处于不可用状态。在FreeBSD和Solaris系统中，资源线程优先级将失效。在Linux系统中，只有配置了CAP_SYS_NICE属性，资源管理优先级才能发挥作用。</p> <p><strong>9.字符集支持</strong> MySQL 8中默认的字符集由 <code>latin1</code> 更改为 <code>utf8mb4</code> ，并首次增加了日语所特定使用的集合，utf8mb4_ja_0900_as_cs。</p> <p><strong>10.优化器增强</strong> MySQL优化器开始支持隐藏索引和降序索引。隐藏索引不会被优化器使用，验证索引的必要性时不需要删除索引，先将索引隐藏，如果优化器性能无影响就可以真正地删除索引。降序索引允许优化器对多个列进行排序，并且允许排序顺序不一致。</p> <p><strong>11.公用表表达式</strong> 公用表表达式（Common Table Expressions）简称为CTE，MySQL现在支持递归和非递归两种形式的CTE。CTE通过在SELECT语句或其他特定语句前 <code>使用WITH语句对临时结果集</code> 进行命名。</p> <p>基础语法如下：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">WITH</span> cte_name <span class="token punctuation">(</span>col_name1<span class="token punctuation">,</span>col_name2 <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> <span class="token punctuation">(</span>Subquery<span class="token punctuation">)</span>
<span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> cte_name<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>Subquery代表子查询，子查询前使用WITH语句将结果集命名为cte_name，在后续的查询中即可使用cte_name进行查询。</p> <p><strong>12.窗口函数</strong> MySQL 8开始支持窗口函数。在之前的版本中已存在的大部分 聚合函数 在MySQL 8中也可以作为窗口函数来使用。</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251930375.png" alt="image-20220613092731201"></p> <p><strong>13.正则表达式支持</strong> MySQL在8.0.4以后的版本中采用支持Unicode的国际化组件库实现正则表达式操作，这种方式不仅能提供完全的Unicode支持，而且是多字节安全编码。MySQL增加了REGEXP_LIKE()、EGEXP_INSTR()、REGEXP_REPLACE()和 REGEXP_SUBSTR()等函数来提升性能。另外，regexp_stack_limit和regexp_time_limit 系统变量能够通过匹配引擎来控制资源消耗。</p> <p><strong>14.内部临时表</strong> <code>TempTable存储引擎取代MEMORY存储引擎成为内部临时表的默认存储引擎</code> 。TempTable存储引擎为VARCHAR和VARBINARY列提供高效存储。internal_tmp_mem_storage_engine会话变量定义了内部临时表的存储引擎，可选的值有两个，TempTable和MEMORY，其中TempTable为默认的存储引擎。temptable_max_ram系统配置项定义了TempTable存储引擎可使用的最大内存数量。</p> <p><strong>15.日志记录</strong> 在MySQL 8中错误日志子系统由一系列MySQL组件构成。这些组件的构成由系统变量
log_error_services来配置，能够实现日志事件的过滤和写入。</p> <p><strong>16.备份锁</strong> 新的备份锁允许在线备份期间执行数据操作语句，同时阻止可能造成快照不一致的操作。新备份锁由 LOCK INSTANCE FOR BACKUP 和 UNLOCK INSTANCE 语法提供支持，执行这些操作需要备份管理员特权。</p> <p><strong>17.增强的MySQL复制</strong> MySQL 8复制支持对 <code>JSON文档</code> 进行部分更新的 <code>二进制日志记录</code> ，该记录 <code>使用紧凑的二进制格式</code>，从而节省记录完整JSON文档的空间。当使用基于语句的日志记录时，这种紧凑的日志记录会自动完成，并且可以通过将新的binlog_row_value_options系统变量值设置为PARTIAL_JSON来启用。</p> <h3 id="_1-2-mysql8-0移除的旧特性"><a href="#_1-2-mysql8-0移除的旧特性" class="header-anchor">#</a> 1.2 MySQL8.0移除的旧特性</h3> <p>在MySQL 5.7版本上开发的应用程序如果使用了MySQL8.0 移除的特性，语句可能会失败，或者产生不同的执行结果。为了避免这些问题，对于使用了移除特性的应用，应当尽力修正避免使用这些特性，并尽可能使用替代方法。</p> <p><strong>1.查询缓存</strong> <code>查询缓存已被移除</code></p> <p>删除的项有：</p> <p><strong>（1）语句</strong>：FLUSH QUERY CACHE和RESET QUERY CACHE。</p> <p><strong>（2）系统变量</strong>：query_cache_limit、query_cache_min_res_unit、query_cache_size、query_cache_type、query_cache_wlock_invalidate。</p> <p><strong>（3）状态变量</strong>：Qcache_free_blocks、Qcache_free_memory、Qcache_hits、Qcache_inserts、Qcache_lowmem_prunes、Qcache_not_cached、Qcache_queries_in_cache、Qcache_total_blocks。</p> <p><strong>（4）线程状态</strong>：checking privileges on cached query、checking query cache for query、invalidating query cache entries、sending cached result to client、storing result in query cache、waiting for query cache lock。</p> <p><strong>2.加密相关</strong> 删除的加密相关的内容有：ENCODE()、DECODE()、ENCRYPT()、DES_ENCRYPT()和DES_DECRYPT()函数，配置项des-key-file，系统变量have_crypt，FLUSH语句的DES_KEY_FILE选项，HAVE_CRYPT CMake选项。 对于移除的ENCRYPT()函数，考虑使用SHA2()替代，对于其他移除的函数，使用AES_ENCRYPT()和AES_DECRYPT()替代。</p> <p><strong>3.空间函数相关</strong> 在MySQL 5.7版本中，多个空间函数已被标记为过时。这些过时函数在MySQL 8中都已被移除，只保留了对应的ST_和MBR函数。</p> <p><strong>4.\N和NULL</strong> 在SQL语句中，解析器不再将\N视为NULL，所以在SQL语句中应使用NULL代替\N。这项变化不会影响使用LOAD DATA INFILE或者SELECT...INTO OUTFILE操作文件的导入和导出。在这类操作中，NULL仍等同于\N。</p> <p><strong>5.mysql_install_db</strong> 在MySQL分布中，已移除了mysql_install_db程序，数据字典初始化需要调用带着--initialize或者--initialize-insecure选项的mysqld来代替实现。另外，--bootstrap和INSTALL_SCRIPTDIR CMake也已被删除。</p> <p><strong>6.通用分区处理程序</strong> 通用分区处理程序已从MySQL服务中被移除。为了实现给定表分区，表所使用的存储引擎需要自有的分区处理程序。 提供本地分区支持的MySQL存储引擎有两个，即InnoDB和NDB，而在MySQL 8中只支持InnoDB。</p> <p><strong>7.系统和状态变量信息</strong> 在INFORMATION_SCHEMA数据库中，对系统和状态变量信息不再进行维护。
GLOBAL_VARIABLES、SESSION_VARIABLES、GLOBAL_STATUS、SESSION_STATUS表都已被删除。另外，系统变量show_compatibility_56也已被删除。被删除的状态变量有Slave_heartbeat_period、
Slave_last_heartbeat,Slave_received_heartbeats、Slave_retried_transactions、Slave_running。以上被删除的内容都可使用性能模式中对应的内容进行替代。</p> <p><strong>8.mysql_plugin工具</strong> mysql_plugin工具用来配置MySQL服务器插件，现已被删除，可使用--plugin-load或--plugin-load-add选项在服务器启动时加载插件或者在运行时使用INSTALL PLUGIN语句加载插件来替代该工具。</p> <h2 id="_2-新特性1-窗口函数"><a href="#_2-新特性1-窗口函数" class="header-anchor">#</a> 2. 新特性1：窗口函数</h2> <h3 id="_2-1-使用窗口函数前后对比"><a href="#_2-1-使用窗口函数前后对比" class="header-anchor">#</a> 2.1 使用窗口函数前后对比</h3> <p>假设我现在有这样一个数据表，它显示了某购物网站在每个城市每个区的销售额：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> sales<span class="token punctuation">(</span>
    id <span class="token keyword">INT</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span>
    city <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    county <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    sales_value <span class="token keyword">DECIMAL</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> sales<span class="token punctuation">(</span>city<span class="token punctuation">,</span>county<span class="token punctuation">,</span>sales_value<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span>
<span class="token punctuation">(</span><span class="token string">'北京'</span><span class="token punctuation">,</span><span class="token string">'海淀'</span><span class="token punctuation">,</span><span class="token number">10.00</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token string">'北京'</span><span class="token punctuation">,</span><span class="token string">'朝阳'</span><span class="token punctuation">,</span><span class="token number">20.00</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token string">'上海'</span><span class="token punctuation">,</span><span class="token string">'黄埔'</span><span class="token punctuation">,</span><span class="token number">30.00</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token string">'上海'</span><span class="token punctuation">,</span><span class="token string">'长宁'</span><span class="token punctuation">,</span><span class="token number">10.00</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>查询：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> sales<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">----+------+--------+-------------+</span>
<span class="token operator">|</span> id <span class="token operator">|</span> city <span class="token operator">|</span> county <span class="token operator">|</span> sales_value <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+------+--------+-------------+</span>
<span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span> 北京 <span class="token operator">|</span> 海淀    <span class="token operator">|</span>      <span class="token number">10</span>     <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span> 北京 <span class="token operator">|</span> 朝阳    <span class="token operator">|</span>      <span class="token number">20</span>     <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span> 上海 <span class="token operator">|</span> 黄埔    <span class="token operator">|</span>      <span class="token number">30</span>     <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">4</span> <span class="token operator">|</span> 上海 <span class="token operator">|</span> 长宁    <span class="token operator">|</span>      <span class="token number">10</span>     <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+------+--------+-------------+</span>
<span class="token number">4</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><strong>需求</strong>：现在计算这个网站在每个城市的销售总额、在全国的销售总额、每个区的销售额占所在城市销售额中的比率，以及占总销售额中的比率。</p> <p>如果用分组和聚合函数，就需要分好几步来计算。</p> <p>第一步，计算总销售金额，并存入临时表 a：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TEMPORARY</span> <span class="token keyword">TABLE</span> a    <span class="token comment">-- 创建临时表</span>
<span class="token keyword">SELECT</span> <span class="token function">SUM</span><span class="token punctuation">(</span>sales_value<span class="token punctuation">)</span> <span class="token keyword">AS</span> sales_value <span class="token comment">-- 计算总计金额</span>
<span class="token keyword">FROM</span> sales<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>查看一下临时表 a ：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> a<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">-------------+</span>
<span class="token operator">|</span> sales_value <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">-------------+</span>
<span class="token operator">|</span>      <span class="token number">70</span>     <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">-------------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>第二步，计算每个城市的销售总额并存入临时表 b：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TEMPORARY</span> <span class="token keyword">TABLE</span> b   <span class="token comment">-- 创建临时表</span>
<span class="token keyword">SELECT</span> city<span class="token punctuation">,</span><span class="token function">SUM</span><span class="token punctuation">(</span>sales_value<span class="token punctuation">)</span> <span class="token keyword">AS</span> sales_value  <span class="token comment">-- 计算城市销售合计</span>
<span class="token keyword">FROM</span> sales
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> city<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>查看临时表 b ：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> b<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">------+-------------+</span>
<span class="token operator">|</span> city <span class="token operator">|</span> sales_value <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+-------------+</span>
<span class="token operator">|</span> 北京 <span class="token operator">|</span>      <span class="token number">30</span>     <span class="token operator">|</span>
<span class="token operator">|</span> 上海 <span class="token operator">|</span>      <span class="token number">40</span>     <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+-------------+</span>
<span class="token number">2</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>第三步，计算各区的销售占所在城市的总计金额的比例，和占全部销售总计金额的比例。我们可以通过下面的连接查询获得需要的结果：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> s<span class="token punctuation">.</span>city <span class="token keyword">AS</span> 城市<span class="token punctuation">,</span>s<span class="token punctuation">.</span>county <span class="token keyword">AS</span> 区<span class="token punctuation">,</span>s<span class="token punctuation">.</span>sales_value <span class="token keyword">AS</span> 区销售额<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> b<span class="token punctuation">.</span>sales_value <span class="token keyword">AS</span> 市销售额<span class="token punctuation">,</span>s<span class="token punctuation">.</span>sales_value<span class="token operator">/</span>b<span class="token punctuation">.</span>sales_value <span class="token keyword">AS</span> 市比率<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> a<span class="token punctuation">.</span>sales_value <span class="token keyword">AS</span> 总销售额<span class="token punctuation">,</span>s<span class="token punctuation">.</span>sales_value<span class="token operator">/</span>a<span class="token punctuation">.</span>sales_value <span class="token keyword">AS</span> 总比率
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> sales s
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">JOIN</span> b <span class="token keyword">ON</span> <span class="token punctuation">(</span>s<span class="token punctuation">.</span>city<span class="token operator">=</span>b<span class="token punctuation">.</span>city<span class="token punctuation">)</span> <span class="token comment">-- 连接市统计结果临时表</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">JOIN</span> a          <span class="token comment">-- 连接总计金额临时表</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> s<span class="token punctuation">.</span>city<span class="token punctuation">,</span>s<span class="token punctuation">.</span>county<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">------+------+----------+----------+--------+----------+--------+</span>
<span class="token operator">|</span> 城市 <span class="token operator">|</span> 区   <span class="token operator">|</span> 区销售额  <span class="token operator">|</span> 市销售额  <span class="token operator">|</span> 市比率  <span class="token operator">|</span> 总销售额  <span class="token operator">|</span> 总比率 <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+------+----------+----------+--------+----------+--------+</span>
<span class="token operator">|</span> 上海 <span class="token operator">|</span> 长宁 <span class="token operator">|</span>    <span class="token number">10</span>    <span class="token operator">|</span>    <span class="token number">40</span>    <span class="token operator">|</span> <span class="token number">0.2500</span>  <span class="token operator">|</span>    <span class="token number">70</span>    <span class="token operator">|</span> <span class="token number">0.1429</span> <span class="token operator">|</span>
<span class="token operator">|</span> 上海 <span class="token operator">|</span> 黄埔 <span class="token operator">|</span>    <span class="token number">30</span>    <span class="token operator">|</span>    <span class="token number">40</span>    <span class="token operator">|</span> <span class="token number">0.7500</span>  <span class="token operator">|</span>    <span class="token number">70</span>    <span class="token operator">|</span> <span class="token number">0.4286</span> <span class="token operator">|</span>
<span class="token operator">|</span> 北京 <span class="token operator">|</span> 朝阳 <span class="token operator">|</span>    <span class="token number">20</span>    <span class="token operator">|</span>    <span class="token number">30</span>    <span class="token operator">|</span> <span class="token number">0.6667</span>  <span class="token operator">|</span>    <span class="token number">70</span>    <span class="token operator">|</span> <span class="token number">0.2857</span> <span class="token operator">|</span>
<span class="token operator">|</span> 北京 <span class="token operator">|</span> 海淀 <span class="token operator">|</span>    <span class="token number">10</span>    <span class="token operator">|</span>    <span class="token number">30</span>    <span class="token operator">|</span> <span class="token number">0.3333</span>  <span class="token operator">|</span>    <span class="token number">70</span>    <span class="token operator">|</span> <span class="token number">0.1429</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+------+----------+----------+--------+----------+--------+</span>
<span class="token number">4</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>结果显示：市销售金额、市销售占比、总销售金额、总销售占比都计算出来了。
同样的查询，如果用窗口函数，就简单多了。我们可以用下面的代码来实现：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> city <span class="token keyword">AS</span> 城市<span class="token punctuation">,</span>county <span class="token keyword">AS</span> 区<span class="token punctuation">,</span>sales_value <span class="token keyword">AS</span> 区销售额<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token function">SUM</span><span class="token punctuation">(</span>sales_value<span class="token punctuation">)</span> <span class="token keyword">OVER</span><span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> city<span class="token punctuation">)</span> <span class="token keyword">AS</span> 市销售额<span class="token punctuation">,</span>  <span class="token comment">-- 计算市销售额</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> sales_value<span class="token operator">/</span><span class="token function">SUM</span><span class="token punctuation">(</span>sales_value<span class="token punctuation">)</span> <span class="token keyword">OVER</span><span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> city<span class="token punctuation">)</span> <span class="token keyword">AS</span> 市比率<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token function">SUM</span><span class="token punctuation">(</span>sales_value<span class="token punctuation">)</span> <span class="token keyword">OVER</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> 总销售额<span class="token punctuation">,</span>  <span class="token comment">-- 计算总销售额</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> sales_value<span class="token operator">/</span><span class="token function">SUM</span><span class="token punctuation">(</span>sales_value<span class="token punctuation">)</span> <span class="token keyword">OVER</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> 总比率
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> sales
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> city<span class="token punctuation">,</span>county<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">------+------+----------+----------+--------+----------+--------+</span>
<span class="token operator">|</span> 城市 <span class="token operator">|</span> 区    <span class="token operator">|</span> 区销售额 <span class="token operator">|</span> 市销售额  <span class="token operator">|</span> 市比率  <span class="token operator">|</span> 总销售额  <span class="token operator">|</span> 总比率 <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+------+----------+----------+--------+----------+--------+</span>
<span class="token operator">|</span> 上海 <span class="token operator">|</span> 长宁  <span class="token operator">|</span>    <span class="token number">10</span>    <span class="token operator">|</span>    <span class="token number">40</span>    <span class="token operator">|</span> <span class="token number">0.2500</span> <span class="token operator">|</span>    <span class="token number">70</span>    <span class="token operator">|</span> <span class="token number">0.1429</span> <span class="token operator">|</span>
<span class="token operator">|</span> 上海 <span class="token operator">|</span> 黄埔  <span class="token operator">|</span>    <span class="token number">30</span>    <span class="token operator">|</span>    <span class="token number">40</span>    <span class="token operator">|</span> <span class="token number">0.7500</span> <span class="token operator">|</span>    <span class="token number">70</span>    <span class="token operator">|</span> <span class="token number">0.4286</span> <span class="token operator">|</span>
<span class="token operator">|</span> 北京 <span class="token operator">|</span> 朝阳  <span class="token operator">|</span>    <span class="token number">20</span>    <span class="token operator">|</span>    <span class="token number">30</span>    <span class="token operator">|</span> <span class="token number">0.6667</span> <span class="token operator">|</span>    <span class="token number">70</span>    <span class="token operator">|</span> <span class="token number">0.2857</span> <span class="token operator">|</span>
<span class="token operator">|</span> 北京 <span class="token operator">|</span> 海淀  <span class="token operator">|</span>    <span class="token number">10</span>    <span class="token operator">|</span>    <span class="token number">30</span>    <span class="token operator">|</span> <span class="token number">0.3333</span> <span class="token operator">|</span>    <span class="token number">70</span>    <span class="token operator">|</span> <span class="token number">0.1429</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+------+----------+-----------+--------+----------+--------+</span>
<span class="token number">4</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>结果显示，我们得到了与上面那种查询同样的结果。
使用窗口函数，只用了一步就完成了查询。而且，由于没有用到临时表，执行的效率也更高了。很显然，<strong>在这种需要用到分组统计的结果对每一条记录进行计算的场景下，使用窗口函数更好</strong>。</p> <h3 id="_2-2-窗口函数分类"><a href="#_2-2-窗口函数分类" class="header-anchor">#</a> 2.2 窗口函数分类</h3> <p>MySQL从8.0版本开始支持窗口函数。窗口函数的作用类似于在查询中对数据进行分组，不同的是，分组
操作会把分组的结果聚合成一条记录，而窗口函数是将结果置于每一条数据记录中。
窗口函数可以分为 <code>静态窗口函数</code> 和 <code>动态窗口函数</code> 。</p> <ul><li>静态窗口函数的窗口大小是固定的，不会因为记录的不同而不同；</li> <li>动态窗口函数的窗口大小会随着记录的不同而变化。
MySQL官方网站窗口函数的网址为https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html#function_row-number。</li></ul> <p>窗口函数总体上可以分为序号函数、分布函数、前后函数、首尾函数和其他函数，如下表：</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251930376.png" alt="image-20220613093918725"></p> <h3 id="_2-3-语法结构"><a href="#_2-3-语法结构" class="header-anchor">#</a> 2.3 语法结构</h3> <p>窗口函数的语法结构是：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>函数 <span class="token keyword">OVER</span>（<span class="token punctuation">[</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> 字段名 <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> 字段名 <span class="token keyword">ASC</span><span class="token operator">|</span><span class="token keyword">DESC</span><span class="token punctuation">]</span>）
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>或者是：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>函数 <span class="token keyword">OVER</span> 窗口名 … WINDOW 窗口名 <span class="token keyword">AS</span> （<span class="token punctuation">[</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> 字段名 <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> 字段名 <span class="token keyword">ASC</span><span class="token operator">|</span><span class="token keyword">DESC</span><span class="token punctuation">]</span>）
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>OVER 关键字指定函数窗口的范围。
<ul><li>如果省略后面括号中的内容，则窗口会包含满足WHERE条件的所有记录，窗口函数会基于所有满足WHERE条件的记录进行计算。</li> <li>如果OVER关键字后面的括号不为空，则可以使用如下语法设置窗口。</li></ul></li> <li>窗口名：为窗口设置一个别名，用来标识窗口。</li> <li>PARTITION BY子句：指定窗口函数按照哪些字段进行分组。分组后，窗口函数可以在每个分组中分别执行。</li> <li>ORDER BY子句：指定窗口函数按照哪些字段进行排序。执行排序操作使窗口函数按照排序后的数据记录的顺序进行编号。</li> <li>FRAME子句：为分区中的某个子集定义规则，可以用来作为滑动窗口使用。</li></ul> <h3 id="_2-4-分类讲解"><a href="#_2-4-分类讲解" class="header-anchor">#</a> 2.4 分类讲解</h3> <p>创建表：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> goods<span class="token punctuation">(</span>
    id <span class="token keyword">INT</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span>
    category_id <span class="token keyword">INT</span><span class="token punctuation">,</span>
    category <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    NAME <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    price <span class="token keyword">DECIMAL</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    stock <span class="token keyword">INT</span><span class="token punctuation">,</span>
    upper_time <span class="token keyword">DATETIME</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>添加数据：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> goods<span class="token punctuation">(</span>category_id<span class="token punctuation">,</span>category<span class="token punctuation">,</span>NAME<span class="token punctuation">,</span>price<span class="token punctuation">,</span>stock<span class="token punctuation">,</span>upper_time<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span>
<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'女装/女士精品'</span><span class="token punctuation">,</span> <span class="token string">'T恤'</span><span class="token punctuation">,</span> <span class="token number">39.90</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">,</span> <span class="token string">'2020-11-10 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'女装/女士精品'</span><span class="token punctuation">,</span> <span class="token string">'连衣裙'</span><span class="token punctuation">,</span> <span class="token number">79.90</span><span class="token punctuation">,</span> <span class="token number">2500</span><span class="token punctuation">,</span> <span class="token string">'2020-11-10 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'女装/女士精品'</span><span class="token punctuation">,</span> <span class="token string">'卫衣'</span><span class="token punctuation">,</span> <span class="token number">89.90</span><span class="token punctuation">,</span> <span class="token number">1500</span><span class="token punctuation">,</span> <span class="token string">'2020-11-10 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'女装/女士精品'</span><span class="token punctuation">,</span> <span class="token string">'牛仔裤'</span><span class="token punctuation">,</span> <span class="token number">89.90</span><span class="token punctuation">,</span> <span class="token number">3500</span><span class="token punctuation">,</span> <span class="token string">'2020-11-10 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'女装/女士精品'</span><span class="token punctuation">,</span> <span class="token string">'百褶裙'</span><span class="token punctuation">,</span> <span class="token number">29.90</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">,</span> <span class="token string">'2020-11-10 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'女装/女士精品'</span><span class="token punctuation">,</span> <span class="token string">'呢绒外套'</span><span class="token punctuation">,</span> <span class="token number">399.90</span><span class="token punctuation">,</span> <span class="token number">1200</span><span class="token punctuation">,</span> <span class="token string">'2020-11-10 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'户外运动'</span><span class="token punctuation">,</span> <span class="token string">'自行车'</span><span class="token punctuation">,</span> <span class="token number">399.90</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">,</span> <span class="token string">'2020-11-10 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'户外运动'</span><span class="token punctuation">,</span> <span class="token string">'山地自行车'</span><span class="token punctuation">,</span> <span class="token number">1399.90</span><span class="token punctuation">,</span> <span class="token number">2500</span><span class="token punctuation">,</span> <span class="token string">'2020-11-10 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'户外运动'</span><span class="token punctuation">,</span> <span class="token string">'登山杖'</span><span class="token punctuation">,</span> <span class="token number">59.90</span><span class="token punctuation">,</span> <span class="token number">1500</span><span class="token punctuation">,</span> <span class="token string">'2020-11-10 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'户外运动'</span><span class="token punctuation">,</span> <span class="token string">'骑行装备'</span><span class="token punctuation">,</span> <span class="token number">399.90</span><span class="token punctuation">,</span> <span class="token number">3500</span><span class="token punctuation">,</span> <span class="token string">'2020-11-10 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'户外运动'</span><span class="token punctuation">,</span> <span class="token string">'运动外套'</span><span class="token punctuation">,</span> <span class="token number">799.90</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">,</span> <span class="token string">'2020-11-10 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'户外运动'</span><span class="token punctuation">,</span> <span class="token string">'滑板'</span><span class="token punctuation">,</span> <span class="token number">499.90</span><span class="token punctuation">,</span> <span class="token number">1200</span><span class="token punctuation">,</span> <span class="token string">'2020-11-10 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>下面针对goods表中的数据来验证每个窗口函数的功能。</p> <h4 id="_1-序号函数"><a href="#_1-序号函数" class="header-anchor">#</a> 1. 序号函数</h4> <p><strong>1．ROW_NUMBER()函数</strong>
ROW_NUMBER()函数能够对数据中的序号进行顺序显示。</p> <p>举例：查询 goods 数据表中每个商品分类下价格降序排列的各个商品信息。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> ROW_NUMBER<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span><span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price <span class="token keyword">DESC</span><span class="token punctuation">)</span> <span class="token keyword">AS</span>
row_num<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> id<span class="token punctuation">,</span> category_id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span> stock
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> goods<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+------------+---------+-------+</span>
<span class="token operator">|</span> row_num <span class="token operator">|</span> id <span class="token operator">|</span> category_id <span class="token operator">|</span> category   	 <span class="token operator">|</span> NAME       <span class="token operator">|</span> price   <span class="token operator">|</span> stock <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+------------+---------+-------+</span>
<span class="token operator">|</span>    <span class="token number">1</span>    <span class="token operator">|</span>  <span class="token number">6</span> <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套    <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>  <span class="token number">1200</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span>    <span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣        <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>  <span class="token number">1500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">3</span>    <span class="token operator">|</span>  <span class="token number">4</span> <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤      <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>  <span class="token number">3500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">4</span>    <span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 连衣裙      <span class="token operator">|</span>  <span class="token number">79.90</span>  <span class="token operator">|</span>  <span class="token number">2500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">5</span>    <span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> T恤         <span class="token operator">|</span>  <span class="token number">39.90</span>  <span class="token operator">|</span> <span class="token number">1000</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">6</span>    <span class="token operator">|</span>  <span class="token number">5</span> <span class="token operator">|</span>      <span class="token number">1</span> 	 <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 百褶裙      <span class="token operator">|</span>  <span class="token number">29.90</span>  <span class="token operator">|</span>  <span class="token number">500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">1</span> 	  <span class="token operator">|</span>  <span class="token number">8</span> <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动    	  <span class="token operator">|</span> 山地自行车  <span class="token operator">|</span> <span class="token number">1399.90</span> <span class="token operator">|</span>  <span class="token number">2500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span>    <span class="token operator">|</span> <span class="token number">11</span> <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动    	  <span class="token operator">|</span> 运动外套    <span class="token operator">|</span>  <span class="token number">799.90</span> <span class="token operator">|</span>  <span class="token number">500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">3</span>    <span class="token operator">|</span> <span class="token number">12</span> <span class="token operator">|</span>      <span class="token number">2</span>  	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 滑板        <span class="token operator">|</span>  <span class="token number">499.90</span> <span class="token operator">|</span>  <span class="token number">1200</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">4</span>    <span class="token operator">|</span>  <span class="token number">7</span> <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 自行车      <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>  <span class="token number">1000</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">5</span>    <span class="token operator">|</span> <span class="token number">10</span> <span class="token operator">|</span>      <span class="token number">2</span>  	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 骑行装备    <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>  <span class="token number">3500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">6</span>    <span class="token operator">|</span>  <span class="token number">9</span> <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 登山杖      <span class="token operator">|</span>  <span class="token number">59.90</span>  <span class="token operator">|</span>  <span class="token number">1500</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+------------+---------+-------+</span>
<span class="token number">12</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>举例：查询 goods 数据表中每个商品分类下价格最高的3种商品信息。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> <span class="token punctuation">(</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span>  <span class="token keyword">SELECT</span> ROW_NUMBER<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span><span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price <span class="token keyword">DESC</span><span class="token punctuation">)</span> <span class="token keyword">AS</span>
row_num<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> id<span class="token punctuation">,</span> category_id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span> stock
 <span class="token operator">-</span><span class="token operator">&gt;</span>  <span class="token keyword">FROM</span> goods<span class="token punctuation">)</span> t
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">WHERE</span> row_num <span class="token operator">&lt;=</span> <span class="token number">3</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+------------+---------+-------+</span>
<span class="token operator">|</span> row_num <span class="token operator">|</span> id <span class="token operator">|</span> category_id <span class="token operator">|</span> category      <span class="token operator">|</span> NAME       <span class="token operator">|</span> price   <span class="token operator">|</span> stock <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+------------+---------+-------+</span>
<span class="token operator">|</span>    <span class="token number">1</span>    <span class="token operator">|</span>  <span class="token number">6</span> <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套    <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>  <span class="token number">1200</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span>    <span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣        <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>  <span class="token number">1500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">3</span>    <span class="token operator">|</span>  <span class="token number">4</span> <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤      <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>  <span class="token number">3500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">1</span>    <span class="token operator">|</span>  <span class="token number">8</span> <span class="token operator">|</span>      <span class="token number">2</span>      <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 山地自行车   <span class="token operator">|</span> <span class="token number">1399.90</span> <span class="token operator">|</span>  <span class="token number">2500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span>    <span class="token operator">|</span> <span class="token number">11</span> <span class="token operator">|</span>      <span class="token number">2</span>      <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 运动外套     <span class="token operator">|</span>  <span class="token number">799.90</span> <span class="token operator">|</span>  <span class="token number">500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">3</span>    <span class="token operator">|</span> <span class="token number">12</span> <span class="token operator">|</span>      <span class="token number">2</span>      <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 滑板         <span class="token operator">|</span>  <span class="token number">499.90</span> <span class="token operator">|</span>  <span class="token number">1200</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+------------+----------+-------+</span>
<span class="token number">6</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>在名称为“女装/女士精品”的商品类别中，有两款商品的价格为89.90元，分别是卫衣和牛仔裤。两款商品的序号都应该为2，而不是一个为2，另一个为3。此时，可以使用RANK()函数和DENSE_RANK()函数解决。</p> <p><strong>2．RANK()函数</strong></p> <p>使用RANK()函数能够对序号进行并列排序，并且会跳过重复的序号，比如序号为1、1、3。</p> <p>举例：使用RANK()函数获取 goods 数据表中各类别的价格从高到低排序的各商品信息。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> RANK<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span><span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price <span class="token keyword">DESC</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> row_num<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> id<span class="token punctuation">,</span> category_id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span> stock
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> goods<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+------------+---------+-------+</span>
<span class="token operator">|</span> row_num <span class="token operator">|</span> id <span class="token operator">|</span> category_id <span class="token operator">|</span> category   	 <span class="token operator">|</span> NAME       <span class="token operator">|</span> price   <span class="token operator">|</span> stock <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+------------+---------+-------+</span>
<span class="token operator">|</span>    <span class="token number">1</span>    <span class="token operator">|</span> <span class="token number">6</span>  <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套    <span class="token operator">|</span> <span class="token number">399.90</span>  <span class="token operator">|</span> <span class="token number">1200</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span>    <span class="token operator">|</span> <span class="token number">3</span>  <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣        <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span> <span class="token number">1500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span>    <span class="token operator">|</span> <span class="token number">4</span>  <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤      <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span> <span class="token number">3500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">4</span>    <span class="token operator">|</span> <span class="token number">2</span>  <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 连衣裙      <span class="token operator">|</span>  <span class="token number">79.90</span>  <span class="token operator">|</span> <span class="token number">2500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">5</span>    <span class="token operator">|</span> <span class="token number">1</span>  <span class="token operator">|</span>      <span class="token number">1</span> 	 <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> T恤         <span class="token operator">|</span>  <span class="token number">39.90</span>  <span class="token operator">|</span> <span class="token number">1000</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">6</span>    <span class="token operator">|</span> <span class="token number">5</span>  <span class="token operator">|</span>      <span class="token number">1</span> 	 <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 百褶裙      <span class="token operator">|</span>  <span class="token number">29.90</span>  <span class="token operator">|</span>  <span class="token number">500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">1</span>    <span class="token operator">|</span> <span class="token number">8</span>  <span class="token operator">|</span>      <span class="token number">2</span>   	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 山地自行车  <span class="token operator">|</span> <span class="token number">1399.90</span> <span class="token operator">|</span> <span class="token number">2500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span>    <span class="token operator">|</span> <span class="token number">11</span> <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 运动外套    <span class="token operator">|</span> <span class="token number">799.90</span>  <span class="token operator">|</span>  <span class="token number">500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">3</span>    <span class="token operator">|</span> <span class="token number">12</span> <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 滑板        <span class="token operator">|</span> <span class="token number">499.90</span>  <span class="token operator">|</span> <span class="token number">1200</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">4</span>    <span class="token operator">|</span> <span class="token number">7</span>  <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 自行车      <span class="token operator">|</span> <span class="token number">399.90</span>  <span class="token operator">|</span> <span class="token number">1000</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">4</span>    <span class="token operator">|</span> <span class="token number">10</span> <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 骑行装备    <span class="token operator">|</span> <span class="token number">399.90</span>  <span class="token operator">|</span> <span class="token number">3500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">6</span>    <span class="token operator">|</span> <span class="token number">9</span>  <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 登山杖      <span class="token operator">|</span>  <span class="token number">59.90</span>  <span class="token operator">|</span> <span class="token number">1500</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+------------+---------+-------+</span>
<span class="token number">12</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p>举例：使用RANK()函数获取 goods 数据表中类别为“女装/女士精品”的价格最高的4款商品信息。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span><span class="token punctuation">(</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span>  <span class="token keyword">SELECT</span> RANK<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span><span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price <span class="token keyword">DESC</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> row_num<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> id<span class="token punctuation">,</span> category_id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span> stock
 <span class="token operator">-</span><span class="token operator">&gt;</span>  <span class="token keyword">FROM</span> goods<span class="token punctuation">)</span> t
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">WHERE</span> category_id <span class="token operator">=</span> <span class="token number">1</span> <span class="token operator">AND</span> row_num <span class="token operator">&lt;=</span> <span class="token number">4</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+----------+--------+-------+</span>
<span class="token operator">|</span> row_num <span class="token operator">|</span> id <span class="token operator">|</span> category_id <span class="token operator">|</span> category      <span class="token operator">|</span> NAME     <span class="token operator">|</span> price  <span class="token operator">|</span> stock <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+----------+--------+-------+</span>
<span class="token operator">|</span>    <span class="token number">1</span> 	  <span class="token operator">|</span>  <span class="token number">6</span> <span class="token operator">|</span>      <span class="token number">1</span> 	 <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套  <span class="token operator">|</span> <span class="token number">399.90</span> <span class="token operator">|</span>  <span class="token number">1200</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span> 	  <span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span>      <span class="token number">1</span> 	 <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣      <span class="token operator">|</span>  <span class="token number">89.90</span> <span class="token operator">|</span>  <span class="token number">1500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span> 	  <span class="token operator">|</span>  <span class="token number">4</span> <span class="token operator">|</span>      <span class="token number">1</span> 	 <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤    <span class="token operator">|</span>  <span class="token number">89.90</span> <span class="token operator">|</span>  <span class="token number">3500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">4</span> 	  <span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span>      <span class="token number">1</span> 	 <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 连衣裙    <span class="token operator">|</span>  <span class="token number">79.90</span> <span class="token operator">|</span>  <span class="token number">2500</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+----------+--------+-------+</span>
<span class="token number">4</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>可以看到，使用RANK()函数得出的序号为1、2、2、4，相同价格的商品序号相同，后面的商品序号是不连续的，跳过了重复的序号。</p> <p><strong>3．DENSE_RANK()函数</strong></p> <p>DENSE_RANK()函数对序号进行并列排序，并且不会跳过重复的序号，比如序号为1、1、2。</p> <p>举例：使用DENSE_RANK()函数获取 goods 数据表中各类别的价格从高到低排序的各商品信息。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> DENSE_RANK<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span><span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price <span class="token keyword">DESC</span><span class="token punctuation">)</span> <span class="token keyword">AS</span>
row_num<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> id<span class="token punctuation">,</span> category_id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span> stock
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> goods<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+------------+---------+-------+</span>
<span class="token operator">|</span> row_num <span class="token operator">|</span> id <span class="token operator">|</span> category_id <span class="token operator">|</span> category      <span class="token operator">|</span> NAME       <span class="token operator">|</span> price   <span class="token operator">|</span> stock <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+------------+---------+-------+</span>
<span class="token operator">|</span>    <span class="token number">1</span>    <span class="token operator">|</span> <span class="token number">6</span>  <span class="token operator">|</span>      <span class="token number">1</span>   	 <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套    <span class="token operator">|</span> <span class="token number">399.90</span>  <span class="token operator">|</span> <span class="token number">1200</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span>    <span class="token operator">|</span> <span class="token number">3</span>  <span class="token operator">|</span>      <span class="token number">1</span> 	 <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣        <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span> <span class="token number">1500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span>    <span class="token operator">|</span> <span class="token number">4</span>  <span class="token operator">|</span>      <span class="token number">1</span> 	 <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤      <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span> <span class="token number">3500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">3</span>    <span class="token operator">|</span> <span class="token number">2</span>  <span class="token operator">|</span>      <span class="token number">1</span> 	 <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 连衣裙      <span class="token operator">|</span>  <span class="token number">79.90</span>  <span class="token operator">|</span> <span class="token number">2500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">4</span>    <span class="token operator">|</span> <span class="token number">1</span>  <span class="token operator">|</span>      <span class="token number">1</span> 	 <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> T恤         <span class="token operator">|</span>  <span class="token number">39.90</span>  <span class="token operator">|</span> <span class="token number">1000</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">5</span>    <span class="token operator">|</span> <span class="token number">5</span>  <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 百褶裙      <span class="token operator">|</span>  <span class="token number">29.90</span>  <span class="token operator">|</span>  <span class="token number">500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">1</span>    <span class="token operator">|</span> <span class="token number">8</span>  <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 山地自行车  <span class="token operator">|</span> <span class="token number">1399.90</span> <span class="token operator">|</span> <span class="token number">2500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span>    <span class="token operator">|</span> <span class="token number">11</span> <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 运动外套    <span class="token operator">|</span> <span class="token number">799.90</span>  <span class="token operator">|</span>  <span class="token number">500</span>  <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">3</span>    <span class="token operator">|</span> <span class="token number">12</span> <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 滑板        <span class="token operator">|</span> <span class="token number">499.90</span>  <span class="token operator">|</span> <span class="token number">1200</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">4</span>    <span class="token operator">|</span> <span class="token number">7</span>  <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动    	  <span class="token operator">|</span> 自行车      <span class="token operator">|</span> <span class="token number">399.90</span>  <span class="token operator">|</span> <span class="token number">1000</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">4</span>    <span class="token operator">|</span> <span class="token number">10</span> <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 骑行装备    <span class="token operator">|</span> <span class="token number">399.90</span>  <span class="token operator">|</span> <span class="token number">3500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">5</span>    <span class="token operator">|</span> <span class="token number">9</span>  <span class="token operator">|</span>      <span class="token number">2</span> 	 <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 登山杖      <span class="token operator">|</span>  <span class="token number">59.90</span>  <span class="token operator">|</span> <span class="token number">1500</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+------------+---------+-------+</span>
<span class="token number">12</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>举例：使用DENSE_RANK()函数获取 goods 数据表中类别为“女装/女士精品”的价格最高的4款商品信息。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span><span class="token punctuation">(</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span>  <span class="token keyword">SELECT</span> DENSE_RANK<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span><span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price <span class="token keyword">DESC</span><span class="token punctuation">)</span> <span class="token keyword">AS</span>
row_num<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> id<span class="token punctuation">,</span> category_id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span> stock
 <span class="token operator">-</span><span class="token operator">&gt;</span>  <span class="token keyword">FROM</span> goods<span class="token punctuation">)</span> t
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">WHERE</span> category_id <span class="token operator">=</span> <span class="token number">1</span> <span class="token operator">AND</span> row_num <span class="token operator">&lt;=</span> <span class="token number">3</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+----------+--------+-------+</span>
<span class="token operator">|</span> row_num <span class="token operator">|</span> id <span class="token operator">|</span> category_id <span class="token operator">|</span> category      <span class="token operator">|</span> NAME     <span class="token operator">|</span> price  <span class="token operator">|</span> stock <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+----------+--------+-------+</span>
<span class="token operator">|</span>    <span class="token number">1</span>    <span class="token operator">|</span>  <span class="token number">6</span> <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套  <span class="token operator">|</span> <span class="token number">399.90</span> <span class="token operator">|</span>  <span class="token number">1200</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span>    <span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣     <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>  <span class="token number">1500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">2</span>    <span class="token operator">|</span>  <span class="token number">4</span> <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤    <span class="token operator">|</span>  <span class="token number">89.90</span> <span class="token operator">|</span>  <span class="token number">3500</span> <span class="token operator">|</span>
<span class="token operator">|</span>    <span class="token number">3</span>    <span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span>      <span class="token number">1</span>      <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 连衣裙    <span class="token operator">|</span>  <span class="token number">79.90</span> <span class="token operator">|</span>  <span class="token number">2500</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+----+-------------+---------------+----------+--------+-------+</span>
<span class="token number">4</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>可以看到，使用DENSE_RANK()函数得出的行号为1、2、2、3，相同价格的商品序号相同，后面的商品序
号是连续的，并且没有跳过重复的序号。</p> <h4 id="_2-分布函数"><a href="#_2-分布函数" class="header-anchor">#</a> 2. 分布函数</h4> <p><strong>1．PERCENT_RANK()函数</strong>
PERCENT_RANK()函数是等级值百分比函数。按照如下方式进行计算。</p> <p>其中，rank的值为使用RANK()函数产生的序号，rows的值为当前窗口的总记录数。</p> <p>举例：计算 goods 数据表中名称为“女装/女士精品”的类别下的商品的PERCENT_RANK值。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token comment">#写法一：</span>
<span class="token keyword">SELECT</span> RANK<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span> <span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price <span class="token keyword">DESC</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> r<span class="token punctuation">,</span>
PERCENT_RANK<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span> <span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price <span class="token keyword">DESC</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> pr<span class="token punctuation">,</span>
id<span class="token punctuation">,</span> category_id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span> stock
<span class="token keyword">FROM</span> goods
<span class="token keyword">WHERE</span> category_id <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token comment">#写法二：</span>
mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> RANK<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span> w <span class="token keyword">AS</span> r<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> PERCENT_RANK<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span> w <span class="token keyword">AS</span> pr<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> id<span class="token punctuation">,</span> category_id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span> stock
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> goods
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">WHERE</span> category_id <span class="token operator">=</span> <span class="token number">1</span> WINDOW w <span class="token keyword">AS</span> <span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price
<span class="token keyword">DESC</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---+-----+----+-------------+---------------+----------+--------+-------+</span>
<span class="token operator">|</span> r <span class="token operator">|</span> pr  <span class="token operator">|</span> id <span class="token operator">|</span> category_id  <span class="token operator">|</span> category     <span class="token operator">|</span> NAME     <span class="token operator">|</span> price  <span class="token operator">|</span> stock <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---+-----+----+-------------+---------------+----------+--------+-------+</span>
<span class="token operator">|</span> <span class="token number">1</span> <span class="token operator">|</span>  <span class="token number">0</span>  <span class="token operator">|</span>  <span class="token number">6</span> <span class="token operator">|</span>      <span class="token number">1</span>       <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套 <span class="token operator">|</span> <span class="token number">399.90</span> <span class="token operator">|</span>  <span class="token number">1200</span> <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">2</span> <span class="token operator">|</span> <span class="token number">0.2</span> <span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span>      <span class="token number">1</span> 	  <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣     <span class="token operator">|</span>  <span class="token number">89.90</span> <span class="token operator">|</span>  <span class="token number">1500</span> <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">2</span> <span class="token operator">|</span> <span class="token number">0.2</span> <span class="token operator">|</span>  <span class="token number">4</span> <span class="token operator">|</span>      <span class="token number">1</span> 	  <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤   <span class="token operator">|</span>  <span class="token number">89.90</span> <span class="token operator">|</span>  <span class="token number">3500</span> <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">4</span> <span class="token operator">|</span> <span class="token number">0.6</span> <span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span>      <span class="token number">1</span> 	  <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 连衣裙   <span class="token operator">|</span>  <span class="token number">79.90</span> <span class="token operator">|</span>  <span class="token number">2500</span> <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">5</span> <span class="token operator">|</span> <span class="token number">0.8</span> <span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span>      <span class="token number">1</span> 	  <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> T恤      <span class="token operator">|</span>  <span class="token number">39.90</span> <span class="token operator">|</span>  <span class="token number">1000</span> <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">6</span> <span class="token operator">|</span>  <span class="token number">1</span>  <span class="token operator">|</span>  <span class="token number">5</span> <span class="token operator">|</span>      <span class="token number">1</span> 	  <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 百褶裙   <span class="token operator">|</span>  <span class="token number">29.90</span> <span class="token operator">|</span>  <span class="token number">500</span>  <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---+-----+----+-------------+---------------+----------+--------+-------+</span>
<span class="token number">6</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><p><strong>2．CUME_DIST()函数</strong>
CUME_DIST()函数主要用于查询小于或等于某个值的比例。</p> <p>举例：查询goods数据表中小于或等于当前价格的比例。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> CUME_DIST<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span><span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price <span class="token keyword">ASC</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> cd<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> goods<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------------------+----+---------------+------------+---------+</span>
<span class="token operator">|</span> cd         		  <span class="token operator">|</span> id <span class="token operator">|</span> category      <span class="token operator">|</span> NAME       <span class="token operator">|</span> price   <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------------+----+---------------+------------+---------+</span>
<span class="token operator">|</span> <span class="token number">0.16666666666666666</span> <span class="token operator">|</span>  <span class="token number">5</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 百褶裙      <span class="token operator">|</span>  <span class="token number">29.90</span>  <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">0.3333333333333333</span> <span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> T恤         <span class="token operator">|</span>  <span class="token number">39.90</span>  <span class="token operator">|</span>
<span class="token operator">|</span>         <span class="token number">0.5</span> 	 	  <span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 连衣裙       <span class="token operator">|</span>  <span class="token number">79.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">0.8333333333333334</span> <span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣    	   <span class="token operator">|</span>  <span class="token number">89.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">0.8333333333333334</span> <span class="token operator">|</span>  <span class="token number">4</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤       <span class="token operator">|</span>  <span class="token number">89.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>          <span class="token number">1</span> 	      <span class="token operator">|</span>  <span class="token number">6</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套     <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">0.16666666666666666</span> <span class="token operator">|</span>  <span class="token number">9</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 登山杖       <span class="token operator">|</span>  <span class="token number">59.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>         <span class="token number">0.5</span> 		  <span class="token operator">|</span>  <span class="token number">7</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 自行车       <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>         <span class="token number">0.5</span> 	      <span class="token operator">|</span> <span class="token number">10</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 骑行装备     <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">0.6666666666666666</span> <span class="token operator">|</span> <span class="token number">12</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 滑板         <span class="token operator">|</span>  <span class="token number">499.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">0.8333333333333334</span> <span class="token operator">|</span> <span class="token number">11</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 运动外套      <span class="token operator">|</span>  <span class="token number">799.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>          <span class="token number">1</span> 	      <span class="token operator">|</span>  <span class="token number">8</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 山地自行车    <span class="token operator">|</span> <span class="token number">1399.90</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------------+----+---------------+------------+---------+</span>
<span class="token number">12</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h4 id="_3-前后函数"><a href="#_3-前后函数" class="header-anchor">#</a> 3. 前后函数</h4> <p><strong>1．LAG(expr,n)函数</strong></p> <p>LAG(expr,n)函数返回当前行的前n行的expr的值。</p> <p>举例：查询goods数据表中前一个商品价格与当前商品价格的差值。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span> pre_price<span class="token punctuation">,</span> price <span class="token operator">-</span> pre_price <span class="token keyword">AS</span> diff_price
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> <span class="token punctuation">(</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span>  <span class="token keyword">SELECT</span> id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span>LAG<span class="token punctuation">(</span>price<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span> w <span class="token keyword">AS</span> pre_price
 <span class="token operator">-</span><span class="token operator">&gt;</span>  <span class="token keyword">FROM</span> goods
 <span class="token operator">-</span><span class="token operator">&gt;</span> WINDOW w <span class="token keyword">AS</span> <span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price<span class="token punctuation">)</span><span class="token punctuation">)</span> t<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+---------+-----------+------------+</span>
<span class="token operator">|</span> id <span class="token operator">|</span> category      <span class="token operator">|</span> NAME       <span class="token operator">|</span> price   <span class="token operator">|</span> pre_price <span class="token operator">|</span> diff_price <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+---------+-----------+------------+</span>
<span class="token operator">|</span>  <span class="token number">5</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 百褶裙      <span class="token operator">|</span>  <span class="token number">29.90</span>  <span class="token operator">|</span>    <span class="token boolean">NULL</span>   <span class="token operator">|</span>    <span class="token boolean">NULL</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> T恤         <span class="token operator">|</span>  <span class="token number">39.90</span>  <span class="token operator">|</span>   <span class="token number">29.90</span>   <span class="token operator">|</span>    <span class="token number">10.00</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 连衣裙      <span class="token operator">|</span>  <span class="token number">79.90</span>  <span class="token operator">|</span>   <span class="token number">39.90</span>   <span class="token operator">|</span>    <span class="token number">40.00</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣    	<span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>   <span class="token number">79.90</span>   <span class="token operator">|</span>    <span class="token number">10.00</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">4</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤      <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>   <span class="token number">89.90</span>   <span class="token operator">|</span>    <span class="token number">0.00</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">6</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套    <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>   <span class="token number">89.90</span>   <span class="token operator">|</span>   <span class="token number">310.00</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">9</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 登山杖   	 <span class="token operator">|</span>  <span class="token number">59.90</span>  <span class="token operator">|</span>    <span class="token boolean">NULL</span>   <span class="token operator">|</span>    <span class="token boolean">NULL</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">7</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 自行车   	 <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>   <span class="token number">59.90</span>   <span class="token operator">|</span>   <span class="token number">340.00</span>  <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">10</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 骑行装备    <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>   <span class="token number">399.90</span>  <span class="token operator">|</span>    <span class="token number">0.00</span>   <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">12</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 滑板    	  <span class="token operator">|</span>  <span class="token number">499.90</span> <span class="token operator">|</span>   <span class="token number">399.90</span>  <span class="token operator">|</span>   <span class="token number">100.00</span>  <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">11</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 运动外套    <span class="token operator">|</span>  <span class="token number">799.90</span> <span class="token operator">|</span>   <span class="token number">499.90</span>  <span class="token operator">|</span>   <span class="token number">300.00</span>  <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">8</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 山地自行车   <span class="token operator">|</span> <span class="token number">1399.90</span> <span class="token operator">|</span>   <span class="token number">799.90</span> <span class="token operator">|</span>   <span class="token number">600.00</span>   <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+---------+-----------+------------+</span>
<span class="token number">12</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><p><strong>2．LEAD(expr,n)函数</strong></p> <p>LEAD(expr,n)函数返回当前行的后n行的expr的值。</p> <p>举例：查询goods数据表中后一个商品价格与当前商品价格的差值。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> behind_price<span class="token punctuation">,</span> price<span class="token punctuation">,</span>behind_price <span class="token operator">-</span> price <span class="token keyword">AS</span>
diff_price
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span><span class="token punctuation">(</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span>  <span class="token keyword">SELECT</span> id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span>LEAD<span class="token punctuation">(</span>price<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span> w <span class="token keyword">AS</span> behind_price
 <span class="token operator">-</span><span class="token operator">&gt;</span>  <span class="token keyword">FROM</span> goods WINDOW w <span class="token keyword">AS</span> <span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price<span class="token punctuation">)</span><span class="token punctuation">)</span> t<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+--------------+---------+------------+</span>
<span class="token operator">|</span> id <span class="token operator">|</span> category      <span class="token operator">|</span> NAME       <span class="token operator">|</span> behind_price <span class="token operator">|</span> price   <span class="token operator">|</span> diff_price <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+--------------+---------+------------+</span>
<span class="token operator">|</span>  <span class="token number">5</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 百褶裙      <span class="token operator">|</span>     <span class="token number">39.90</span>    <span class="token operator">|</span>  <span class="token number">29.90</span>  <span class="token operator">|</span>    <span class="token number">10.00</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> T恤        <span class="token operator">|</span>     <span class="token number">79.90</span>     <span class="token operator">|</span>  <span class="token number">39.90</span>  <span class="token operator">|</span>    <span class="token number">40.00</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 连衣裙      <span class="token operator">|</span>     <span class="token number">89.90</span>    <span class="token operator">|</span>  <span class="token number">79.90</span>  <span class="token operator">|</span>    <span class="token number">10.00</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣        <span class="token operator">|</span>     <span class="token number">89.90</span>    <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>    <span class="token number">0.00</span>     <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">4</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤      <span class="token operator">|</span>    <span class="token number">399.90</span>    <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>   <span class="token number">310.00</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">6</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套   <span class="token operator">|</span>     <span class="token boolean">NULL</span>      <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>    <span class="token boolean">NULL</span>     <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">9</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 登山杖      <span class="token operator">|</span>    <span class="token number">399.90</span>     <span class="token operator">|</span>  <span class="token number">59.90</span>  <span class="token operator">|</span>   <span class="token number">340.00</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">7</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 自行车      <span class="token operator">|</span>    <span class="token number">399.90</span>     <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>    <span class="token number">0.00</span>    <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">10</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 骑行装备    <span class="token operator">|</span>    <span class="token number">499.90</span>     <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>   <span class="token number">100.00</span>   <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">12</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 滑板        <span class="token operator">|</span>    <span class="token number">799.90</span>     <span class="token operator">|</span>  <span class="token number">499.90</span> <span class="token operator">|</span>   <span class="token number">300.00</span>   <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">11</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 运动外套    <span class="token operator">|</span>    <span class="token number">1399.90</span>    <span class="token operator">|</span>  <span class="token number">799.90</span> <span class="token operator">|</span>   <span class="token number">600.00</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">8</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 山地自行车  <span class="token operator">|</span>     <span class="token boolean">NULL</span>      <span class="token operator">|</span> <span class="token number">1399.90</span> <span class="token operator">|</span>    <span class="token boolean">NULL</span>    <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+--------------+---------+------------+</span>
<span class="token number">12</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h4 id="_4-首尾函数"><a href="#_4-首尾函数" class="header-anchor">#</a> 4. 首尾函数</h4> <p><strong>1．FIRST_VALUE(expr)函数</strong></p> <p>FIRST_VALUE(expr)函数返回第一个expr的值。</p> <p>举例：按照价格排序，查询第1个商品的价格信息。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span> stock<span class="token punctuation">,</span>FIRST_VALUE<span class="token punctuation">(</span>price<span class="token punctuation">)</span> <span class="token keyword">OVER</span> w <span class="token keyword">AS</span>
first_price
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> goods WINDOW w <span class="token keyword">AS</span> <span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+---------+-------+-------------+</span>
<span class="token operator">|</span> id <span class="token operator">|</span> category      <span class="token operator">|</span> NAME    	  <span class="token operator">|</span> price   <span class="token operator">|</span> stock <span class="token operator">|</span> first_price <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+---------+-------+-------------+</span>
<span class="token operator">|</span>  <span class="token number">5</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 百褶裙      <span class="token operator">|</span>  <span class="token number">29.90</span>  <span class="token operator">|</span>  <span class="token number">500</span>  <span class="token operator">|</span>    <span class="token number">29.90</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> T恤        <span class="token operator">|</span>  <span class="token number">39.90</span>   <span class="token operator">|</span>  <span class="token number">1000</span> <span class="token operator">|</span>    <span class="token number">29.90</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 连衣裙      <span class="token operator">|</span>  <span class="token number">79.90</span>  <span class="token operator">|</span>  <span class="token number">2500</span> <span class="token operator">|</span>    <span class="token number">29.90</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣        <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>  <span class="token number">1500</span> <span class="token operator">|</span>    <span class="token number">29.90</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">4</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤      <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>  <span class="token number">3500</span> <span class="token operator">|</span>    <span class="token number">29.90</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">6</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套     <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>  <span class="token number">1200</span> <span class="token operator">|</span>    <span class="token number">29.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">9</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 登山杖       <span class="token operator">|</span>  <span class="token number">59.90</span>  <span class="token operator">|</span>  <span class="token number">1500</span> <span class="token operator">|</span>    <span class="token number">59.90</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">7</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 自行车       <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>  <span class="token number">1000</span> <span class="token operator">|</span>    <span class="token number">59.90</span>    <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">10</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 骑行装备     <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>  <span class="token number">3500</span> <span class="token operator">|</span>    <span class="token number">59.90</span>    <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">12</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 滑板         <span class="token operator">|</span>  <span class="token number">499.90</span> <span class="token operator">|</span>  <span class="token number">1200</span> <span class="token operator">|</span>    <span class="token number">59.90</span>    <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">11</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 运动外套      <span class="token operator">|</span>  <span class="token number">799.90</span> <span class="token operator">|</span>  <span class="token number">500</span> <span class="token operator">|</span>    <span class="token number">59.90</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">8</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 山地自行车    <span class="token operator">|</span> <span class="token number">1399.90</span> <span class="token operator">|</span>  <span class="token number">2500</span> <span class="token operator">|</span>    <span class="token number">59.90</span>   <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+---------+-------+-------------+</span>
<span class="token number">12</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p><strong>2．LAST_VALUE(expr)函数</strong></p> <p>LAST_VALUE(expr)函数返回最后一个expr的值。</p> <p>举例：按照价格排序，查询最后一个商品的价格信息。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span> stock<span class="token punctuation">,</span>LAST_VALUE<span class="token punctuation">(</span>price<span class="token punctuation">)</span> <span class="token keyword">OVER</span> w <span class="token keyword">AS</span> last_price
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> goods WINDOW w <span class="token keyword">AS</span> <span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+---------+-------+------------+</span>
<span class="token operator">|</span> id <span class="token operator">|</span> category      <span class="token operator">|</span> NAME       <span class="token operator">|</span> price   <span class="token operator">|</span> stock <span class="token operator">|</span> last_price <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+---------+-------+------------+</span>
<span class="token operator">|</span>  <span class="token number">5</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 百褶裙      <span class="token operator">|</span>  <span class="token number">29.90</span>  <span class="token operator">|</span>  <span class="token number">500</span>  <span class="token operator">|</span>    <span class="token number">29.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> T恤        <span class="token operator">|</span>  <span class="token number">39.90</span>  <span class="token operator">|</span>  <span class="token number">1000</span> <span class="token operator">|</span>    <span class="token number">39.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 连衣裙      <span class="token operator">|</span>  <span class="token number">79.90</span> <span class="token operator">|</span>  <span class="token number">2500</span>  <span class="token operator">|</span>    <span class="token number">79.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣       <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>  <span class="token number">1500</span>  <span class="token operator">|</span>    <span class="token number">89.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">4</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤     <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>  <span class="token number">3500</span>  <span class="token operator">|</span>    <span class="token number">89.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">6</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套   <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>  <span class="token number">1200</span>  <span class="token operator">|</span>   <span class="token number">399.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">9</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 登山杖      <span class="token operator">|</span>  <span class="token number">59.90</span>  <span class="token operator">|</span>  <span class="token number">1500</span> <span class="token operator">|</span>    <span class="token number">59.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">7</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 自行车      <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>  <span class="token number">1000</span> <span class="token operator">|</span>   <span class="token number">399.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">10</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 骑行装备    <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>  <span class="token number">3500</span> <span class="token operator">|</span>   <span class="token number">399.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">12</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 滑板       <span class="token operator">|</span>  <span class="token number">499.90</span>  <span class="token operator">|</span>  <span class="token number">1200</span> <span class="token operator">|</span>   <span class="token number">499.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">11</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 运动外套    <span class="token operator">|</span>  <span class="token number">799.90</span> <span class="token operator">|</span>  <span class="token number">500</span>  <span class="token operator">|</span>   <span class="token number">799.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">8</span> <span class="token operator">|</span> 户外运动      <span class="token operator">|</span> 山地自行车  <span class="token operator">|</span> <span class="token number">1399.90</span> <span class="token operator">|</span>  <span class="token number">2500</span> <span class="token operator">|</span>   <span class="token number">1399.90</span>   <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+---------+-------+------------+</span>
<span class="token number">12</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h4 id="_5-其他函数"><a href="#_5-其他函数" class="header-anchor">#</a> 5. 其他函数</h4> <p><strong>1．NTH_VALUE(expr,n)函数</strong></p> <p>NTH_VALUE(expr,n)函数返回第n个expr的值。</p> <p>举例：查询goods数据表中排名第2和第3的价格信息。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price<span class="token punctuation">,</span>NTH_VALUE<span class="token punctuation">(</span>price<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span> w <span class="token keyword">AS</span> second_price<span class="token punctuation">,</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> NTH_VALUE<span class="token punctuation">(</span>price<span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span> w <span class="token keyword">AS</span> third_price
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> goods WINDOW w <span class="token keyword">AS</span> <span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+---------+--------------+-------------+</span>
<span class="token operator">|</span> id <span class="token operator">|</span> category   	 <span class="token operator">|</span> NAME    	  <span class="token operator">|</span> price   <span class="token operator">|</span> second_price <span class="token operator">|</span> third_price <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+---------+--------------+-------------+</span>
<span class="token operator">|</span>  <span class="token number">5</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 百褶裙      <span class="token operator">|</span>  <span class="token number">29.90</span>  <span class="token operator">|</span>     <span class="token boolean">NULL</span>     <span class="token operator">|</span>     <span class="token boolean">NULL</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> T恤         <span class="token operator">|</span>  <span class="token number">39.90</span> <span class="token operator">|</span>     <span class="token number">39.90</span>     <span class="token operator">|</span>     <span class="token boolean">NULL</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 连衣裙       <span class="token operator">|</span>  <span class="token number">79.90</span> <span class="token operator">|</span>     <span class="token number">39.90</span>    <span class="token operator">|</span>    <span class="token number">79.90</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣        <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>     <span class="token number">39.90</span>    <span class="token operator">|</span>    <span class="token number">79.90</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">4</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤      <span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>     <span class="token number">39.90</span>     <span class="token operator">|</span>    <span class="token number">79.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">6</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套    <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>     <span class="token number">39.90</span>     <span class="token operator">|</span>    <span class="token number">79.90</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">9</span> <span class="token operator">|</span> 户外运动    	  <span class="token operator">|</span> 登山杖      <span class="token operator">|</span>  <span class="token number">59.90</span>  <span class="token operator">|</span>     <span class="token boolean">NULL</span>     <span class="token operator">|</span>     <span class="token boolean">NULL</span>    <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">7</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 自行车      <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>    <span class="token number">399.90</span>    <span class="token operator">|</span>    <span class="token number">399.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">10</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 骑行装备    <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>    <span class="token number">399.90</span>    <span class="token operator">|</span>    <span class="token number">399.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">12</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 滑板        <span class="token operator">|</span>  <span class="token number">499.90</span> <span class="token operator">|</span>    <span class="token number">399.90</span>    <span class="token operator">|</span>    <span class="token number">399.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">11</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 运动外套    <span class="token operator">|</span>  <span class="token number">799.90</span> <span class="token operator">|</span>    <span class="token number">399.90</span>    <span class="token operator">|</span>    <span class="token number">399.90</span>   <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">8</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 山地自行车  <span class="token operator">|</span> <span class="token number">1399.90</span> <span class="token operator">|</span>    <span class="token number">399.90</span>    <span class="token operator">|</span>    <span class="token number">399.90</span>   <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+---------------+------------+---------+--------------+-------------+</span>
<span class="token number">12</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p><strong>2．NTILE(n)函数</strong></p> <p>NTILE(n)函数将分区中的有序数据分为n个桶，记录桶编号。</p> <p>举例：将goods表中的商品按照价格分为3组。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> NTILE<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span> w <span class="token keyword">AS</span> nt<span class="token punctuation">,</span>id<span class="token punctuation">,</span> category<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> price
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> goods WINDOW w <span class="token keyword">AS</span> <span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> category_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> price<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">----+----+---------------+------------+---------+</span>
<span class="token operator">|</span> nt <span class="token operator">|</span> id <span class="token operator">|</span> category      <span class="token operator">|</span> NAME       <span class="token operator">|</span> price   <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+----+---------------+------------+---------+</span>
<span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span>  <span class="token number">5</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 百褶裙   	<span class="token operator">|</span>  <span class="token number">29.90</span>  <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> T恤         <span class="token operator">|</span>  <span class="token number">39.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 连衣裙      <span class="token operator">|</span>  <span class="token number">79.90</span>  <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 卫衣    	 <span class="token operator">|</span>  <span class="token number">89.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span>  <span class="token number">4</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 牛仔裤   	<span class="token operator">|</span>  <span class="token number">89.90</span>  <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span>  <span class="token number">6</span> <span class="token operator">|</span> 女装<span class="token operator">/</span>女士精品  <span class="token operator">|</span> 呢绒外套    <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span>  <span class="token number">9</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 登山杖   	  <span class="token operator">|</span>  <span class="token number">59.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span>  <span class="token number">7</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 自行车      <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span> <span class="token number">10</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 骑行装备    <span class="token operator">|</span>  <span class="token number">399.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span> <span class="token number">12</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 滑板        <span class="token operator">|</span>  <span class="token number">499.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span> <span class="token number">11</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 运动外套    <span class="token operator">|</span>  <span class="token number">799.90</span> <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span>  <span class="token number">8</span> <span class="token operator">|</span> 户外运动       <span class="token operator">|</span> 山地自行车  <span class="token operator">|</span> <span class="token number">1399.90</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+----+---------------+------------+---------+</span>
<span class="token number">12</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h3 id="_2-5-小-结"><a href="#_2-5-小-结" class="header-anchor">#</a> 2.5 小 结</h3> <p>窗口函数的特点是可以分组，而且可以在分组内排序。另外，窗口函数不会因为分组而减少原表中的行数，这对我们在原表数据的基础上进行统计和排序非常有用。</p> <h2 id="_3-新特性2-公用表表达式"><a href="#_3-新特性2-公用表表达式" class="header-anchor">#</a> 3. 新特性2：公用表表达式</h2> <p>公用表表达式（或通用表表达式）简称为CTE（Common Table Expressions）。CTE是一个命名的临时结果集，作用范围是当前语句。CTE可以理解成一个可以复用的子查询，当然跟子查询还是有点区别的，CTE可以引用其他CTE，但子查询不能引用其他子查询。所以，可以考虑代替子查询。</p> <p>依据语法结构和执行方式的不同，公用表表达式分为 <code>普通公用表表达式</code> 和 <code>递归公用表表达式</code> 2 种。</p> <h3 id="_3-1-普通公用表表达式"><a href="#_3-1-普通公用表表达式" class="header-anchor">#</a> 3.1 普通公用表表达式</h3> <p>普通公用表表达式的语法结构是：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">WITH</span> CTE名称
<span class="token keyword">AS</span> （子查询）
<span class="token keyword">SELECT</span><span class="token operator">|</span><span class="token keyword">DELETE</span><span class="token operator">|</span><span class="token keyword">UPDATE</span> 语句<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>普通公用表表达式类似于子查询，不过，跟子查询不同的是，它可以被多次引用，而且可以被其他的普通公用表表达式所引用。</p> <p>举例：查询员工所在的部门的详细信息。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> departments
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">WHERE</span> department_id <span class="token operator">IN</span> <span class="token punctuation">(</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span>          <span class="token keyword">SELECT</span> <span class="token keyword">DISTINCT</span> department_id
 <span class="token operator">-</span><span class="token operator">&gt;</span>          <span class="token keyword">FROM</span> employees
 <span class="token operator">-</span><span class="token operator">&gt;</span>         <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------------+------------------+------------+-------------+</span>
<span class="token operator">|</span> department_id <span class="token operator">|</span> department_name  <span class="token operator">|</span> manager_id <span class="token operator">|</span> location_id <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------+------------------+------------+-------------+</span>
<span class="token operator">|</span>       <span class="token number">10</span> 		<span class="token operator">|</span> Administration   <span class="token operator">|</span>     <span class="token number">200</span>    <span class="token operator">|</span>     <span class="token number">1700</span> 	  <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">20</span> 		<span class="token operator">|</span> Marketing    	   <span class="token operator">|</span>     <span class="token number">201</span>    <span class="token operator">|</span>     <span class="token number">1800</span>    <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">30</span> 		<span class="token operator">|</span> Purchasing       <span class="token operator">|</span>     <span class="token number">114</span>    <span class="token operator">|</span>     <span class="token number">1700</span>    <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">40</span> 		<span class="token operator">|</span> Human Resources  <span class="token operator">|</span>     <span class="token number">203</span>    <span class="token operator">|</span>     <span class="token number">2400</span>    <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">50</span> 		<span class="token operator">|</span> Shipping         <span class="token operator">|</span>     <span class="token number">121</span>    <span class="token operator">|</span>     <span class="token number">1500</span>    <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">60</span> 		<span class="token operator">|</span> IT               <span class="token operator">|</span>     <span class="token number">103</span>    <span class="token operator">|</span>     <span class="token number">1400</span>    <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">70</span> 		<span class="token operator">|</span> <span class="token keyword">Public</span> Relations <span class="token operator">|</span>     <span class="token number">204</span>    <span class="token operator">|</span>     <span class="token number">2700</span>    <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">80</span> 		<span class="token operator">|</span> Sales            <span class="token operator">|</span>     <span class="token number">145</span>    <span class="token operator">|</span>     <span class="token number">2500</span>    <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">90</span> 		<span class="token operator">|</span> Executive        <span class="token operator">|</span>     <span class="token number">100</span>    <span class="token operator">|</span>     <span class="token number">1700</span>    <span class="token operator">|</span>
<span class="token operator">|</span>      <span class="token number">100</span> 		<span class="token operator">|</span> Finance          <span class="token operator">|</span>     <span class="token number">108</span>    <span class="token operator">|</span>     <span class="token number">1700</span>    <span class="token operator">|</span>
<span class="token operator">|</span>      <span class="token number">110</span> 		<span class="token operator">|</span> Accounting       <span class="token operator">|</span>     <span class="token number">205</span>    <span class="token operator">|</span>     <span class="token number">1700</span>    <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------+------------------+------------+-------------+</span>
<span class="token number">11</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>这个查询也可以用普通公用表表达式的方式完成：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">WITH</span> emp_dept_id
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">AS</span> <span class="token punctuation">(</span><span class="token keyword">SELECT</span> <span class="token keyword">DISTINCT</span> department_id <span class="token keyword">FROM</span> employees<span class="token punctuation">)</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> departments d <span class="token keyword">JOIN</span> emp_dept_id e
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">ON</span> d<span class="token punctuation">.</span>department_id <span class="token operator">=</span> e<span class="token punctuation">.</span>department_id<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------------+------------------+------------+-------------+---------------+</span>
<span class="token operator">|</span> department_id <span class="token operator">|</span> department_name  <span class="token operator">|</span> manager_id <span class="token operator">|</span> location_id <span class="token operator">|</span> department_id <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------+------------------+------------+-------------+---------------+</span>
<span class="token operator">|</span>       <span class="token number">90</span> 		<span class="token operator">|</span> Executive    	   <span class="token operator">|</span>     <span class="token number">100</span> 	<span class="token operator">|</span>     <span class="token number">1700</span> 	  <span class="token operator">|</span>       <span class="token number">90</span> 	  <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">60</span> 		<span class="token operator">|</span> IT        	   <span class="token operator">|</span>     <span class="token number">103</span> 	<span class="token operator">|</span>     <span class="token number">1400</span> 	  <span class="token operator">|</span>       <span class="token number">60</span> 	  <span class="token operator">|</span>
<span class="token operator">|</span>      <span class="token number">100</span> 		<span class="token operator">|</span> Finance     	   <span class="token operator">|</span>     <span class="token number">108</span> 	<span class="token operator">|</span>     <span class="token number">1700</span> 	  <span class="token operator">|</span>      <span class="token number">100</span> 	  <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">30</span> 		<span class="token operator">|</span> Purchasing       <span class="token operator">|</span>     <span class="token number">114</span> 	<span class="token operator">|</span>     <span class="token number">1700</span> 	  <span class="token operator">|</span>       <span class="token number">30</span> 	  <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">50</span> 		<span class="token operator">|</span> Shipping         <span class="token operator">|</span>     <span class="token number">121</span> 	<span class="token operator">|</span>     <span class="token number">1500</span>    <span class="token operator">|</span>       <span class="token number">50</span> 	  <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">80</span> 		<span class="token operator">|</span> Sales            <span class="token operator">|</span>     <span class="token number">145</span> 	<span class="token operator">|</span>     <span class="token number">2500</span>    <span class="token operator">|</span>       <span class="token number">80</span> 	  <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">10</span> 		<span class="token operator">|</span> Administration   <span class="token operator">|</span>     <span class="token number">200</span> 	<span class="token operator">|</span>     <span class="token number">1700</span>    <span class="token operator">|</span>       <span class="token number">10</span> 	  <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">20</span> 		<span class="token operator">|</span> Marketing        <span class="token operator">|</span>     <span class="token number">201</span> 	<span class="token operator">|</span>     <span class="token number">1800</span>    <span class="token operator">|</span>       <span class="token number">20</span> 	  <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">40</span> 		<span class="token operator">|</span> Human Resources  <span class="token operator">|</span>     <span class="token number">203</span> 	<span class="token operator">|</span>     <span class="token number">2400</span>    <span class="token operator">|</span>       <span class="token number">40</span> 	  <span class="token operator">|</span>
<span class="token operator">|</span>       <span class="token number">70</span> 		<span class="token operator">|</span> <span class="token keyword">Public</span> Relations <span class="token operator">|</span>     <span class="token number">204</span> 	<span class="token operator">|</span>     <span class="token number">2700</span>    <span class="token operator">|</span>       <span class="token number">70</span> 	  <span class="token operator">|</span>
<span class="token operator">|</span>      <span class="token number">110</span> 		<span class="token operator">|</span> Accounting       <span class="token operator">|</span>     <span class="token number">205</span> 	<span class="token operator">|</span>     <span class="token number">1700</span>    <span class="token operator">|</span>      <span class="token number">110</span> 	  <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------+------------------+------------+-------------+---------------+</span>
<span class="token number">11</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>例子说明，公用表表达式可以起到子查询的作用。以后如果遇到需要使用子查询的场景，你可以在查询之前，先定义公用表表达式，然后在查询中用它来代替子查询。而且，跟子查询相比，公用表表达式有一个优点，就是定义过公用表表达式之后的查询，可以像一个表一样多次引用公用表表达式，而子查询则不能。</p> <h3 id="_3-2-递归公用表表达式"><a href="#_3-2-递归公用表表达式" class="header-anchor">#</a> 3.2 递归公用表表达式</h3> <p>递归公用表表达式也是一种公用表表达式，只不过，除了普通公用表表达式的特点以外，它还有自己的特点，<code>就是可以调用自己</code>。它的语法结构是：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">WITH</span> RECURSIVE
CTE名称 <span class="token keyword">AS</span> （子查询）
<span class="token keyword">SELECT</span><span class="token operator">|</span><span class="token keyword">DELETE</span><span class="token operator">|</span><span class="token keyword">UPDATE</span> 语句<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>递归公用表表达式由 2 部分组成，分别是种子查询和递归查询，中间通过关键字 UNION [ALL]进行连接。</p> <p>这里的<strong>种子查询，意思就是获得递归的初始值</strong>。这个查询只会运行一次，以创建初始数据集，之后递归查询会一直执行，直到没有任何新的查询数据产生，递归返回。</p> <p><strong>案例</strong>：针对于我们常用的employees表，包含employee_id，last_name和manager_id三个字段。如果a是b的管理者，那么，我们可以把b叫做a的下属，如果同时b又是c的管理者，那么c就是b的下属，是a的下下属。</p> <p>下面我们尝试用查询语句列出所有具有下下属身份的人员信息。</p> <p>如果用我们之前学过的知识来解决，会比较复杂，至少要进行 4 次查询才能搞定：</p> <ul><li>第一步，先找出初代管理者，就是不以任何别人为管理者的人，把结果存入临时表；</li> <li>第二步，找出所有以初代管理者为管理者的人，得到一个下属集，把结果存入临时表；</li> <li>第三步，找出所有以下属为管理者的人，得到一个下下属集，把结果存入临时表。</li> <li>第四步，找出所有以下下属为管理者的人，得到一个结果集。</li></ul> <p>如果第四步的结果集为空，则计算结束，第三步的结果集就是我们需要的下下属集了，否则就必须继续进行第四步，一直到结果集为空为止。比如上面的这个数据表，就需要到第五步，才能得到空结果集。而且，最后还要进行第六步：把第三步和第四步的结果集合并，这样才能最终获得我们需要的结果集。</p> <p>如果用递归公用表表达式，就非常简单了。我介绍下具体的思路。</p> <ul><li>用递归公用表表达式中的种子查询，找出初代管理者。字段 n 表示代次，初始值为 1，表示是第一代管理者。</li> <li>用递归公用表表达式中的递归查询，查出以这个递归公用表表达式中的人为管理者的人，并且代次的值加 1。直到没有人以这个递归公用表表达式中的人为管理者了，递归返回。</li> <li>在最后的查询中，选出所有代次大于等于 3 的人，他们肯定是第三代及以上代次的下属了，也就是下下属了。这样就得到了我们需要的结果集。</li></ul> <p>这里看似也是 3 步，实际上是一个查询的 3 个部分，只需要执行一次就可以了。而且也不需要用临时表保存中间结果，比刚刚的方法简单多了。</p> <p><strong>代码实现：</strong></p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">WITH</span> RECURSIVE cte
<span class="token keyword">AS</span>
<span class="token punctuation">(</span>
    <span class="token keyword">SELECT</span> employee_id<span class="token punctuation">,</span>last_name<span class="token punctuation">,</span>manager_id<span class="token punctuation">,</span><span class="token number">1</span> <span class="token keyword">AS</span> n <span class="token keyword">FROM</span> employees <span class="token keyword">WHERE</span> employee_id <span class="token operator">=</span> <span class="token number">100</span>
    <span class="token comment">-- 种子查询，找到第一代领导</span>
    <span class="token keyword">UNION</span> <span class="token keyword">ALL</span>
    <span class="token keyword">SELECT</span> a<span class="token punctuation">.</span>employee_id<span class="token punctuation">,</span>a<span class="token punctuation">.</span>last_name<span class="token punctuation">,</span>a<span class="token punctuation">.</span>manager_id<span class="token punctuation">,</span>n<span class="token operator">+</span><span class="token number">1</span> <span class="token keyword">FROM</span> employees <span class="token keyword">AS</span> a <span class="token keyword">JOIN</span> cte
    <span class="token keyword">ON</span> <span class="token punctuation">(</span>a<span class="token punctuation">.</span>manager_id <span class="token operator">=</span> cte<span class="token punctuation">.</span>employee_id<span class="token punctuation">)</span> <span class="token comment">-- 递归查询，找出以递归公用表表达式的人为领导的人</span>
<span class="token punctuation">)</span>
<span class="token keyword">SELECT</span> employee_id<span class="token punctuation">,</span>last_name <span class="token keyword">FROM</span> cte <span class="token keyword">WHERE</span> n <span class="token operator">&gt;=</span> <span class="token number">3</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>总之，递归公用表表达式对于查询一个有共同的根节点的树形结构数据，非常有用。它可以不受层级的限制，轻松查出所有节点的数据。如果用其他的查询方式，就比较复杂了。</p> <h3 id="_3-3-小-结"><a href="#_3-3-小-结" class="header-anchor">#</a> 3.3 小 结</h3> <p>公用表表达式的作用是可以替代子查询，而且可以被多次引用。递归公用表表达式对查询有一个共同根节点的树形结构数据非常高效，可以轻松搞定其他查询方式难以处理的查询。</p></div></div> <!----> <div class="page-edit"><div class="edit-link"><a href="https://github.com/lcfqzd/vuepress-theme-vdoing/edit/master/docs/04.数据库/01.MySQL笔记/18.MySQL8其它新特性.md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/noteslibrary/tags/?tag=MySQL" title="标签">#MySQL</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/noteslibrary/pages/fe941e/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">触发器</div></a> <!----></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/noteslibrary/pages/fe941e/" class="prev">触发器</a></span> <!----></p></div></div></div> <!----></main></div> <div class="footer"><!----> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2018-2023
    <span>LCFQZD | MIT License</span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"></div></div>
    <script src="/noteslibrary/assets/js/app.dc0c3d24.js" defer></script><script src="/noteslibrary/assets/js/2.a664539c.js" defer></script><script src="/noteslibrary/assets/js/105.235036be.js" defer></script>
  </body>
</html>
